Java solution xD


  • -1
    J
     public boolean isBalanced(TreeNode root) {
        return helper(root, 0)>=0;
    }
    
    public int helper(TreeNode root, int count) {
        if (root==null) {
            return 0;
        }
        int countL = helper(root.left, count);
        countL++;
        int countR = helper(root.right, count);
        countR++;
        if (Math.abs(countL-countR)>1) return -10000; // lol, this solution will fxxked up if the tree has more than 10000 levels looooool
        return (countL>=countR)?countL:countR;
    }

Log in to reply
 

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