My Easy Solution


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

Log in to reply
 

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