Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Binary Tree Preorder Traversal
Bookmark
Recursive
Iterative
Morris
Input
Small (5 nodes)
Example 1
Complete Tree
Custom
tree
=
[4, 2, 5, 1, 3]
4
2
5
1
3
call stack
visit root, then recurse left, then right
4
2
5
1
3
call stack
visit root, then recurse left, then right
4
2
5
1
3
call stack
4
result
4
visit
4
→ append to result
4
2
5
1
3
call stack
4
2
result
4
2
visit
2
→ append to result
4
2
5
1
3
call stack
4
2
result
4
2
1
subtree done → return, pop the call stack
4
2
5
1
3
call stack
4
2
3
result
4
2
1
3
visit
3
→ append to result
4
2
5
1
3
call stack
4
2
result
4
2
1
3
subtree done → return, pop the call stack
4
2
5
1
3
call stack
4
result
4
2
1
3
subtree done → return, pop the call stack
4
2
5
1
3
call stack
4
result
4
2
1
3
5
subtree done → return, pop the call stack
4
2
5
1
3
call stack
result
4
2
1
3
5
subtree done → return, pop the call stack
4
2
5
1
3
call stack
result
4
2
1
3
5
[4, 2, 1, 3, 5]
algo
master
.
io
Step:
Start: preorder = visit root, recurse left, recurse right
0 / 11
Input
Small (5 nodes)
Example 1
Complete Tree
Custom
tree
=
[4, 2, 5, 1, 3]
0 / 11
algo
master
.
io
Step:
Start: preorder = visit root, recurse left, recurse right