31: Balanced Binary Tree

/*
         * Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
         * */
        
        //判断二叉平衡树:
        //
      

 public boolean isBalanced(TreeNode root) {
             if(root==null)
             {
                 return false;
             }
             if (root.right==null&&root.left==null)
             {
                 return true;
             }
             int a = maxDepth(root.left);
             int b = maxDepth(root.right);
             boolean suba = isBalanced(root.left);
             boolean subb = isBalanced(root.right);
             if(Math.abs(a-b)>1)
             {
                 return false;
             }
             if (suba && subb)    
             {
                 return true;
             }
             return false;
        }

 

posted @ 2015-01-27 21:44  Star Yoda  阅读(101)  评论(0编辑  收藏  举报