reconstructing a tree from its preorder and postorder lists

前端 未结 7 1891
既然无缘
既然无缘 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 04:53

    It is not possible to construct a general Binary Tree from preorder and postorder traversals (See this). But if know that the Binary Tree is Full, we can construct the tree without ambiguity. Let us understand this with the help of following example.

    Let us consider the two given arrays as pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7} and post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1}; In pre[], the leftmost element is root of tree. Since the tree is full and array size is more than 1. The value next to 1 in pre[], must be left child of root. So we know 1 is root and 2 is left child. How to find the all nodes in left subtree? We know 2 is root of all nodes in left subtree. All nodes before 2 in post[] must be in left subtree. Now we know 1 is root, elements {8, 9, 4, 5, 2} are in left subtree, and the elements {6, 7, 3} are in right subtree.

                  1
                /   \
               /      \
     {8, 9, 4, 5, 2}     {6, 7, 3}
    

    We recursively follow the above approach and get the following tree.

          1
        /   \
      2       3
    /  \     /  \
    

    4 5 6 7 / \
    8 9

提交回复
热议问题