Simple Java Recurrsion


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

Log in to reply
 

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