-
Notifications
You must be signed in to change notification settings - Fork 17
/
Que39.java
51 lines (34 loc) · 1 KB
/
Que39.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @author: hyl
* @date: 2019/08/17
**/
public class Que39 {
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
/**
* 遍历每个结点,借助一个获取树深度的递归函数,
* 根据该结点的左右子树高度差判断是否平衡,然后递归地对左右子树进行判断。
* @param root
* @return
*/
public boolean IsBalanced_Solution(TreeNode root) {
if (root == null){
return true;
}
return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1
&& IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
//求出root节点的最深深度
private int maxDepth(TreeNode root) {
if (root == null){
return 0;
}
return 1 + Math.max(maxDepth(root.left) , maxDepth(root.right));
}
}