Simple Java Solution


  • 1
    L

    public class Solution {
    public boolean isBalanced(TreeNode root) {

        if(root==null){
            return true;
        }
        
        int L = maxDepth(root.left);
        int R = maxDepth(root.right);
        
        int difference = Math.abs(L-R);
        
        return difference <= 1&&isBalanced(root.left)&&isBalanced(root.right);
    }
    
    public int maxDepth(TreeNode root){
        if(root==null){
            return 0;
        }
        int L = maxDepth(root.left) +1;
        int R = maxDepth(root.right) +1;
        
        return Math.max(L,R);
    }
    

    }


Log in to reply
 

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