Recursive Java solution


  • 0
    W
    public class Solution {
        public boolean isBalanced(TreeNode root) {
            return height(root) != -1;
        }
        
        int height(TreeNode root) {
            if (root == null)
                return 0;
            int left = height(root.left);
            if(left == -1)
                return -1;
            int right = height(root.right);
            if(right == -1)
                return -1;
            if(Math.abs(left - right) < 2)
                return Math.max(left, right) + 1;
            return -1;
        }
    }

Log in to reply
 

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