Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Balanced Binary Tree
Bookmark
Brute Force
Bottom-Up DFS
Iterative Stack
Input
Balanced (Example 1)
Unbalanced (Example 2)
Complete Tree (Balanced)
Custom
tree
=
[3, 9, 20, null, null, 15, 7]
3
9
20
15
7
balanced: |height(L) - height(R)| <= 1 everywhere
3
9
20
15
7
balanced: |height(L) - height(R)| <= 1 everywhere
3
9
20
15
7
checkHeight(
9
)
3
9
20
15
7
recurse right of
9
3
9
20
15
7
h=1
recurse right of
3
3
9
20
15
7
h=1
recurse left of
15
0
3
9
20
15
7
h=1
null child: height
0
3
9
20
15
7
h=1
h=1
checkHeight(
7
)
0
3
9
20
15
7
h=1
h=1
null child: height
0
3
9
20
15
7
h=1
h=1
h=1
|
1
-
1
| =
0
<= 1
3
9
20
15
7
h=3
h=1
h=2
h=1
h=1
return true: tree is balanced
algo
master
.
io
Step:
Start: is the tree height-balanced at every node?
0 / 33
Input
Balanced (Example 1)
Unbalanced (Example 2)
Complete Tree (Balanced)
Custom
tree
=
[3, 9, 20, null, null, 15, 7]
0 / 33
algo
master
.
io
Step:
Start: is the tree height-balanced at every node?