Easy JAVA solution


  • 0
    S
    class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root == null) return true;
            int diff = Math.abs((getLevel(root.left) - getLevel(root.right)));
            return  diff <= 1? isBalanced(root.left) && isBalanced(root.right): false;
        }
        
        private int getLevel(TreeNode root){
            if(root == null) return 0;
            return Math.max(1 + getLevel(root.left), 1 + getLevel(root.right));
        }                       
    }
    

Log in to reply
 

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