-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solution.java
31 lines (28 loc) · 998 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
// 自上而下而下递归思路,比较左树与右树高度差
public boolean isBalanced(TreeNode root) {
if (null == root) {
return true;
}
int leftDepth = recrusionTreeDepth(root.left, 0);
int rightDepth = recrusionTreeDepth(root.right, 0);
return Math.abs(leftDepth - rightDepth) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
private int recrusionTreeDepth(TreeNode node, int depth) {
if (node == null) {
return depth;
}
depth++;
int leftDepth = recrusionTreeDepth(node.left, depth);
int rightDepth = recrusionTreeDepth(node.right, depth);
return Math.max(leftDepth, rightDepth);
}
//官方给出的计算树的高度的方法
public int height(TreeNode root) {
if (root == null) {
return 0;
} else {
return Math.max(height(root.left), height(root.right)) + 1;
}
}
}