reconstructing a tree from its preorder and postorder lists

Preorder and postorder do not uniquely define a tree.

In general, a single tree traversal does not uniquely define the
structure of the tree. For example, as we have seen, for both
the following trees, an inorder traversal yields [1,2,3,4,5,6].

    4                     3
   / \                   / \
  2   5                 2   5
 / \   \               /   / \
1   3   6             1   4   6

The same ambiguity is present for preorder and postorder
traversals. The preorder traversal for the first tree above is
[4,2,1,3,5,6]. Here is a different tree with the same preorder
traversal.

    4
   / \
  2   1
     / \
    3   6
     \
      5

Similarly, we can easily construct another tree whose postorder
traversal [1,3,2,6,5,4] matches that of the first tree above.

Leave a Comment