平衡二叉树

2018-08-29  本文已影响12人  稀饭粥95
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]);
        }
}
上一篇下一篇

猜你喜欢

热点阅读