there is a bug in the test cases in this question. Try my code.


  • 0
    Z
    public class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root==null) return true;
            Queue<TreeNode> q = new LinkedList<TreeNode>();
            q.add(root);
            int num = 1;
            while(num==q.size() ) {
                for(int i=0;i<num;++i) {
                    TreeNode temp = q.poll();
                    if(temp.left!=null) q.add(temp.left);
                    if(temp.right!=null) q.add(temp.right);
                }
                num *= 2;
            }
            num = q.size();
            for(int i=0;i<num;++i) {
                TreeNode temp = q.poll();
                if(temp.left!=null) q.add(temp.left);
                if(temp.right!=null) q.add(temp.right);
            }
            if(q.size()==0) return true;
            return false;
        }
    }
    

  • 0
    Z

    test case 218 is not a height balanced tree.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.