Learn
Practice
Newsletter
Resources
Mobile
New
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Binary Tree Preorder Traversal
Bookmark
Input
Small (5 nodes)
Example 1
Complete Tree
Custom
tree
=
[4, 2, 5, 1, 3]
4
2
5
1
3
stack
result
preorder:
root
then left then right
4
2
5
1
3
stack
result
preorder:
root
then left then right
4
2
5
1
3
stack
result
pop
off the stack
4
1
2
5
1
3
stack
result
4
found right child
5
4
1
2
5
1
3
stack
5
2
top
result
4
push
2
on top: left pops next
4
1
2
2
5
1
3
stack
5
top
result
4
2
result +=
2
4
1
2
2
5
1
3
stack
5
3
top
result
4
2
push
3
first: right pops last
4
1
2
2
5
1
3
stack
5
3
1
top
result
4
2
push
1
on top: left pops next
4
1
2
2
5
1
3
3
stack
5
top
result
4
2
1
pop
off the stack
4
1
2
2
5
1
3
3
4
stack
result
4
2
1
3
pop
off the stack
4
1
2
2
5
5
1
3
3
4
stack
result
4
2
1
3
5
return [4, 2, 1, 3, 5]
algo
master
.
io
Step:
Start: preorder traversal (root, left, right) with a stack
0 / 20
Input
Small (5 nodes)
Example 1
Complete Tree
Custom
tree
=
[4, 2, 5, 1, 3]
0 / 20
algo
master
.
io
Step:
Start: preorder traversal (root, left, right) with a stack