int height(TreeNode root) {
if (root == null) return 0;
return Math.max(height(root.left), height(root.right)) + 1;
}
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
平衡二叉樹判定
1.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
反射獲取Class對象的四種方法
LVAmber
2020-07-08 12:00:21
Java package包,導入包import
LVAmber
2020-07-08 12:00:21
Java俄羅斯輪盤死亡遊戲
LVAmber
2020-07-08 12:00:21
LeetCode_圓圈中最後剩下的數字_Math_E
面試題62. 圓圈中最後剩下的數字 class Solution { public int lastRemaining(int n, int m) { // 模擬法 List<Integer> l
放羊的大飞
2020-07-08 10:58:59
LeetCode_單詞的壓縮編碼_Array_M
放羊的大飞
2020-07-08 10:58:58
LeetCode_地圖分析_BFS_M
放羊的大飞
2020-07-08 10:58:58
LeetCode_卡牌分組_Array_E
放羊的大飞
2020-07-08 10:58:58
LeetCode_兩數之和_Array_E
1. 兩數之和 暴力: class Solution { public int[] twoSum(int[] nums, int target) { int[] res = new int[2];
放羊的大飞
2020-07-08 10:58:58
LeetCode_字符串轉換整數 (atoi)_String_M
放羊的大飞
2020-07-08 10:58:58
LeetCode_旋轉矩陣_Array_M
01.07. 旋轉矩陣 簡單粗暴: class Solution { public void rotate(int[][] matrix) { int size = matrix.length;
放羊的大飞
2020-07-08 10:58:58
LeetCode_盛最多水的容器_Array_M
11. 盛最多水的容器 暴力... class Solution { public int maxArea(int[] height) { int max = 0; for (int i
放羊的大飞
2020-07-08 10:58:58
LeetCode_除自身以外數組的乘積_Array_M
放羊的大飞
2020-07-08 10:58:58
LeetCode_整數反轉_Math_E
7. 整數反轉 class Solution { public int reverse(int x) { long result = 0; while (x != 0) { // 如果是-1
放羊的大飞
2020-07-08 10:58:58
LeetCode_有效的括號_Stack_E
放羊的大飞
2020-07-08 10:58:58