1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public boolean IsBalanced_Solution (TreeNode pRoot) {
if(pRoot == null){
return true;
}
int left = depth(pRoot.left);
int right = depth(pRoot.right);
return Math.abs(left-right)<=1&&IsBalanced_Solution(pRoot.left)&&IsBalanced_Solution(pRoot.right);
}
public int depth(TreeNode root) {
if(root == null){
return 0;
}
return Math.max(depth(root.left),depth(root.right))+1;
}
|