reconstructing a tree from its preorder and postorder lists

前端 未结 7 1886
既然无缘
既然无缘 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 05:07

    As already pointed out by others, a binary tree can not be reconstructed by using only pre and post order traversal. A single child node has ambiguous traversals that cannot identify whether it is left or right child e.g. consider following preorder and postorder traversals: preorder: a,b postorder b,a

    It can produce both of the following trees

    a a \ / b b It is simply not possible to know if b is a's left or right child without any additional information like inorder traversal.

提交回复
热议问题