public class Solution {
public boolean isBalanced(TreeNode root ,int[] depth ){
if(root==null){
depth[0]=0;
return true;
}
int []da = new int[1];
int []db = new int[1];
if(isBalanced(root.left,da)&&
isBalanced(root.right,db)){
if(Math.abs(da[0]-db[0])<=1){
int max = da[0]>db[0]?da[0]:db[0];
depth[0] = 1 + max;
return true;
}
}
return false;
}
public boolean IsBalanced_Solution(TreeNode root) {
return isBalanced(root,new int[1]);
}
}
网友评论