1ms Java solution using global variable


  • 0
    T
    class Solution {
        boolean result = true;
        public boolean isBalanced(TreeNode root) 
        {
            diff(root);
            return result;
        }
        
        public int diff(TreeNode node)
        {
            if (node == null) return 0;
            int left = diff(node.left);
            int right = diff(node.right);
            if (Math.abs(left - right) > 1) result = false;
            return 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.