5-line concise java

  • 1
        public boolean isBalanced(TreeNode root) {
            return helper(root, 0)>=0;
        public int helper(TreeNode root, int cnt){
            if(root==null) return cnt;
            int left = helper(root.left, cnt+1), right = helper(root.right, cnt+1);
            if(left==-1||right==-1||Math.abs(left-right)>1) return -1;
            return Math.max(left, right);

Log in to reply

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