# 110. Balanced Binary Tree (divide & conquer)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mj_-CjW39t1LzffhFF9%2F-Mj_0rv4d2pelOWFKxNL%2Fimage.png?alt=media\&token=5429557b-8694-48e5-911a-e5337665fbd0)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mj_-CjW39t1LzffhFF9%2F-Mj_0udMejwJ1yVKnn3j%2Fimage.png?alt=media\&token=46366f2f-6c1a-4d50-bef0-3e077e7b1686)

based on 104

time: O(n)

space: O(n)

```java
/**
 * 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&#x20;

this one use -1 to represent not balanced

```java
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
```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
```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;
    }
}
```
````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/tree/110.-balanced-binary-tree-divide-and-conquer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
