Accepted simple Java solution


  • 0
    M
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }
    
    public int getHeight(TreeNode root){
        if(root == null) return 0;
        int left = getHeight(root.left), right = getHeight(root.right);
        return (left == -1 || right == -1) ? -1 : (Math.abs(left - right) > 1 ? -1 : Math.max(left, right) + 1);
    }

Log in to reply
 

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