A preorder tree traversal processes each node in a different order.
- Visit the node
- Process the left subtree preorder
- Process the right subtree preorder
The only difference is that we visit the root first, then go left, then right. The preorder output of the same binary tree will be:
26 14 9 21 79 53 35 99 87
-
A postorder tree traversal has this order:
- Process the left subtree postorder
- Process the right subtree postorder
- Visit the node
The prefix “post” refers to after, hence the location of visiting the node after the recursive calls. The printout of the same tree will be as follows:
9 21 14 35 53 87 99 79 26