Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Invert Binary Tree
Bookmark
Recursive DFS
BFS Level-Order
Iterative Stack
Input
Example 1
Simple Tree
Left Child Only
Custom
tree
=
[4, 2, 7, 1, 3, 6, 9]
4
2
7
1
3
6
9
stack
invert: swap children at every node
4
2
7
1
3
6
9
stack
invert: swap children at every node
4
2
7
1
3
6
9
node
stack
swap
2
↔
7
4
2
7
1
3
6
9
node
stack
7
2
top
push new right
2
4
2
7
1
3
6
9
node
stack
7
top
swap
1
↔
3
4
2
7
1
3
6
9
node
stack
7
3
1
top
push new right
1
4
2
7
1
3
6
9
node
stack
7
top
pop
3
4
2
7
1
3
6
9
node
stack
swap
6
↔
9
4
2
7
1
3
6
9
node
stack
9
top
push new left
9
4
2
7
1
3
6
9
node
stack
9
top
leaf
6
: nothing to swap
4
2
7
1
3
6
9
stack
tree inverted, return root
algo
master
.
io
Step:
Start: invert the tree by swapping children at every node
0 / 25
Input
Example 1
Simple Tree
Left Child Only
Custom
tree
=
[4, 2, 7, 1, 3, 6, 9]
0 / 25
algo
master
.
io
Step:
Start: invert the tree by swapping children at every node