reconstructing a tree from its preorder and postorder lists

前端 未结 7 1858
既然无缘
既然无缘 2020-11-28 04:26

Consider the situation where you have two lists of nodes of which all you know is that one is a representation of a preorder traversal of some tree and the other a represent

相关标签:
7条回答
  • 2020-11-28 05:14

    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.

    0 讨论(0)
提交回复
热议问题