My easy java solution with run time 297ms


  • 0
    P
    public class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root==null) return true;
            result rootBalance = findBalance(root);
            return rootBalance.balanced;
            
        }
        
        public result findBalance(TreeNode root){
            if(root==null){
                return new result(true,0);
            }
            result leftChild = findBalance(root.left);
            result rightChild = findBalance(root.right);
            int l=leftChild.depth;
            int r=rightChild.depth;
            int diff_depth=Math.abs(l-r);
            if(leftChild.balanced==false||!rightChild.balanced||diff_depth>1) return new result(false,0);
            return new result(true,l>r?l+1:r+1);
            
        }
        
        public class result{
            public boolean balanced;
            public int depth;
            public result(boolean y, int z){
                balanced=y;
                depth=z;
            }
        
        }
    }

Log in to reply
 

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