Skip to content

Commit 872c9c4

Browse files
committed
Balanced Binary Tree
1 parent e7e72fc commit 872c9c4

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| 0076 | Hard | Minimum Window Substring | Hash Table, String, Sliding Window | [solution](./docs/0076-Mininum-Window-Substring.md) |
2525
| 0084 | Hard | Largest Rectangle in Histogram | Array, Stack, Monotonic Stack | [solution](./docs/0084-Largest-Rectangle-In-Histogram.md) |
2626
| 0104 | Easy | Maximum Depth of Binary Tree | Tree, Depth-First Search, Breadth-First Search, Binary Tree | [solution](./docs/0104-Maximum-Depth-of-Binary-Tree.md) |
27+
| 0110 | Easy | Balanced Binary Tree | Tree, Depth-First Search, Binary Tree | [solution](./docs/0110-Balanced-Binary-Tree.md) |
2728
| 0111 | Easy | Minimum Depth of Binary Tree | Tree, Depth-First Search, Breadth-First Search, Binary Tree | [solution](./docs/0111-Minimum-Depth-of-Binary-Tree.md) |
2829
| 0121 | Easy | Best Time to Buy and Sell Stock | Array | [solution](./docs/0121-Best-Time-to-Buy-and-Sell-Stock.md) |
2930
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |

docs/0110-Balanced-Binary-Tree.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/description/)
2+
3+
## Intuition
4+
5+
A binary tree is considered height-balanced if the depth of the left and right subtrees of every node differ by no more
6+
than one. To check if a tree is balanced, we can use a depth-first search (DFS) to compute the height of each subtree
7+
and simultaneously check for balance at each node.
8+
9+
## Approach
10+
11+
1. **DFS for Height and Balance Check:**
12+
- Use a helper method `dfsHeight to recursively calculate the height of each subtree.
13+
- For each node, calculate the heights of its left and right subtrees.
14+
- If the difference in height between the left and right subtrees is greater than 1, set a result flag to false,
15+
indicating the tree is not balanced.
16+
- Return the height of the current node as `1 + max(left, right)` to propagate the height up the recursion stack.
17+
2. **Global Balance Tracking:**
18+
- Use a boolean array result initialized to true. If any node is found to be unbalanced, the result flag is set to
19+
false.
20+
- Start the recursive check from the root node.
21+
3. **Return the Result:** After the recursion completes, result[0] will indicate whether the tree is balanced.
22+
23+
## Complexity
24+
25+
- **Time Complexity: `O(n`**, where `n` is the number of nodes in the tree. Each node is visited once to calculate its
26+
height and check for balance.
27+
- **Space Complexity: `O(h)`**, where `h` is is the height of the tree. This represents the space used by the recursive
28+
call stack. In the worst case (skewed tree), `h = n`, and in the best case (balanced tree), `h=log(n)`.
29+
30+
## Summary
31+
32+
This solution determines if a binary tree is balanced by recursively calculating the height of each subtree while
33+
checking the balance condition at each node. By tracking the balance in a boolean array, we ensure efficient checking
34+
with a single traversal. The approach meets the requirements with `O(n) time complexity.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.dksifoua.leetcode.balancedbinarytree;
2+
3+
import io.dksifoua.leetcode.utils.TreeNode;
4+
5+
public class Solution {
6+
7+
private int dfsHeight(TreeNode root, boolean[] result) {
8+
if (root == null) {
9+
return 0;
10+
}
11+
12+
int left = this.dfsHeight(root.getLeft(), result);
13+
int right = this.dfsHeight(root.getRight(), result);
14+
15+
if (Math.abs(left - right) > 1) {
16+
result[0] = false;
17+
}
18+
19+
return 1 + Math.max(left, right);
20+
}
21+
22+
public boolean isBalanced(TreeNode root) {
23+
boolean[] result = new boolean[] { true };
24+
this.dfsHeight(root, result);
25+
return result[0];
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.dksifoua.leetcode.balancedbinarytree;
2+
3+
import io.dksifoua.leetcode.utils.TreeNode;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
public class SolutionTest {
10+
11+
final Solution solution = new Solution();
12+
13+
@Test
14+
void test1() {
15+
assertTrue(solution.isBalanced(TreeNode.build(new Integer[] { 3, 9, 20, null, null, 15, 7 })));
16+
}
17+
18+
@Test
19+
void test2() {
20+
assertFalse(solution.isBalanced(TreeNode.build(new Integer[] { 1, 2, 2, 3, 3, null, null, 4, 4 })));
21+
}
22+
}

0 commit comments

Comments
 (0)