JAVA - 1ms


  • 0
    N
    public class Solution {
        public boolean isBalanced(TreeNode root) {
            return height(root)!=-1;
        }
        
        public int height(TreeNode node){
            if (node==null) return 0;
            
            int leftHeigh = height(node.left);
            if (leftHeigh == -1) return -1;
            
            int rightHeigh = height(node.right);
            if (rightHeigh == -1) return -1;
            
            return Math.abs(leftHeigh - rightHeigh) > 1 ? -1 : Math.max(leftHeigh, rightHeigh) + 1;
        }
    }
    

Log in to reply
 

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