Do my codes using dfs?


  • 0
    C
      public boolean isBalanced(TreeNode root) {
            if (root == null) {
                return true;
            }
            if (Math.abs(maxDepth(root.left)-maxDepth(root.right))<=1) {
                return isBalanced(root.left) && isBalanced(root.right);
            }else {
                return false;
            }
            
        }
        
        public int maxDepth(TreeNode node){
            if (node == null) {
                return 0;
            }
            return 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.