My Java solution


  • 0
    W
    public class Solution {
        boolean ret;
        public boolean isBalanced(TreeNode root) {
            ret = true;
            helper(root);
            return ret;
        }
        
        public int helper(TreeNode root){
            if(root == null) return 1;
            int temp1 = helper(root.left);
            int temp2 = helper(root.right);
            if(Math.abs(temp1-temp2) > 1) ret = false;
            return Math.max(temp1+1, temp2+1);
        }
    }
    

Log in to reply
 

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