Post-order Traversal: Left, Right, Root!

  • Explain post-order traversal.

This is the strategy for post-order traversal:

For every node, visit its left subtree, then visit its right subtree, then visit the node.

Following the strategy above will generate this sequence:

4,2,8,7,5,14,12,20,25,21,17,9 4, 2, 8, 7, 5, 14, 12, 20, 25, 21, 17, 9

Explanation

We start with the root, 99, but we must first visit its left subtree. So we move to 55, but we must first visit its left subtree. So we move to 22. We must visit the left subtree of 22, but there is none. So we move to visit the right subtree of 22, which is rooted at 44. We must, however, first visit the left subtree of 44. Since there is no left subtree to 44, we move to visit its right subtree. There is, however, no subtree to the right of 44. So we can visit 44 itself. Now we are done with the right subtree of 22, so we can visit 22 itself.

Now we are done with the left subtree of 55. We must visit the right subtree of 55 now. So we move to 77, but we must first visit its left subtree. There is no subtree to the left of 77, though. So we can move to the right subtree of 77. Therefore, we move to 88. We must now visit the left subtree of 88, but there is none. So we move to visit the right subtree of 88, but there is none. We can, therefore, visit 88 itself. Once 88 is visited, we are done with the right subtree of 77, so we can visit 77 itself. Once we have visited 77, we are done with the right subtree of 55, so we can visit 55 itself. Once we have visited 55, we are done with the left subtree of 99, so we must move to visit the right subtree of 99 \dots

Here is a recursive definition of post-order traversal: for each node, recursively perform post-order traversal of its children (the subtrees rooted at its left and right child, in that order), then visit the node itself.

Resources