14 Tree Traversal
Preorder
A
/ \
B C
/ \ \
D E F
We visit nodes in the order: Root → Left → Right
- Visit A
- Move to left subtree (B)
- Visit B
- Move to left subtree (D) → Visit D
- Move to right subtree (E) → Visit E
- Back to A, move to right subtree (C)
- Visit C
- Move to right subtree (F) → Visit F
Postorder Traversal Steps:
We visit nodes in the order: Left → Right → Root
- Start at A
- Move to left subtree (B)
- Move to left subtree (D) → Visit D
- Move to right subtree (E) → Visit E
- Done with B’s children → Visit B
- Move to right subtree (C)
- C has no left child
- Move to right subtree (F) → Visit F
- Done with C’s children → Visit C
- Done with A’s children → Visit A
Level-order Traversal Steps:
Not possible recursively
We visit nodes level by level, from top to bottom, left to right within each level.
- Level 0: Visit A
- Level 1: Visit B, C
- Level 2: Visit D, E, F
On BST
5
/ \
3 8
/ \ \
1 4 9
Preorder:
Postorder: