Traversals
589. N-ary Tree Preorder Traversal
Easy·
2 Approachesclick to switch
1
DFS (Recursive)
O(n)
O(h)
2
Iterative + Stack
O(n)
O(n)
FIG. N ARY TREE PREORDER TRAVERSAL● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
recvisits each of thennodes exactly once.- Space
- O(h)
- The recursion call stack depth equals tree height
h.
590. N-ary Tree Postorder Traversal
Easy·
2 Approachesclick to switch
1
DFS (Recursive)
O(n)
O(h)
2
Iterative + Stack
O(n)
O(n)
FIG. N ARY TREE POSTORDER TRAVERSAL● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
recvisits every node exactly once.- Space
- O(h)
- The recursion call stack grows to the tree's height
h.
429. N-ary Tree Level Order Traversal
Medium·
3 Approachesclick to switch
1
Iterative + Queue
O(n)
O(w)
2
DFS (Recursive)
O(n)
O(h)
3
DFS (Iterative)
O(n)
O(n)
FIG. N ARY TREE LEVEL ORDER TRAVERSAL● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
- Each node is popped off
queueand visited exactly once. - Space
- O(w)
queueholds one complete level at a time, bounded by the tree's max widthw.