Java clean solution


  • 0
    R
    public class Solution {
    public int help(TreeNode node){
        if(node==null){
            return 0;
        }
        else{
            int left=help(node.left);
            int right=help(node.right);
            if(left<0||right<0) return -1;
            else{
                if(Math.abs(left-right)>1) return -1;
                else return Math.max(left+1,right+1);
            }
        }
    }
    public boolean isBalanced(TreeNode root) {
        return help(root)>=0;
    }
    

    }


Log in to reply
 

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