110. Balanced Binary Tree (divide & conquer)

based on 104

time: O(n)

space: O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    boolean result = true;
    public boolean isBalanced(TreeNode root) {
        maxDepth(root);
        return result;
    }
    private int maxDepth(TreeNode root) {
        if (root == null) return 0;
        
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        
        if (Math.abs(leftDepth - rightDepth) > 1) {
            result = false;
        }
        return 1 + Math.max(leftDepth, rightDepth);
        
    }
}

no global variable

this one use -1 to represent not balanced

class Solution {
    
    public boolean isBalanced(TreeNode root) {
        return maxDepth(root) != -1;
    }
    private int maxDepth(TreeNode root) {
        if (root == null) return 0;
        
        int leftDepth = maxDepth(root.left);
        if (leftDepth == -1) return -1;
        
        int rightDepth = maxDepth(root.right);
        if (rightDepth == -1) return -1;
       
        return Math.abs(leftDepth - rightDepth) > 1 ? -1 : 1 + Math.max(leftDepth, rightDepth);
        
    }
}

use 2 variable return value (more formal

```java
class Solution {
    public boolean isBalanced(TreeNode root) {
       Result result = dfs(root);
       return result.isHeightBalanced;
    }
    class Result {
        boolean isHeightBalanced;
        int height;
        Result(boolean isHeightBalanced, int height) {
            this.isHeightBalanced = isHeightBalanced;
            this.height = height;
        }
    }
    private Result dfs(TreeNode node) {
        if (node == null) {
            return new Result(true, 0);
        }
        Result leftResult = dfs(node.left);
        int left = leftResult.height;

        Result rightResult = dfs(node.right);
        int right = rightResult.height;
        
        int height = Math.max(left, right) + 1;
        boolean isHeightBalanced = (Math.abs(left - right) <= 1) && 
            leftResult.isHeightBalanced && rightResult.isHeightBalanced;
        
        if (!isHeightBalanced) { // early return
            return new Result(isHeightBalanced, height);
        }
        return new Result(isHeightBalanced, height);
    }
}
```

using array to avoid global variable

```java
class Solution {
    public boolean isBalanced(TreeNode root) {
        boolean[] isBalanced = new boolean[1];
        isBalanced[0] = true;
        dfs(root, isBalanced);
        return isBalanced[0];
    }
    private int dfs(TreeNode node, boolean[] isBalanced) {
        if (node == null) {
            return 0;
        }
        int left = dfs(node.left, isBalanced);
        int right = dfs(node.right, isBalanced);
        if (Math.abs(left - right) > 1) {
            isBalanced[0] = false;
        }
        return Math.max(left, right) + 1;
    }
}
```

Last updated