For example, we are provided with only post order traversal array or only pre order traversal array. Can we reconstruct the binary tree back? If we know that the binary tree is full. Moreover, if it is not, is it possible to construct the full binary if know both preorder and post order at the same time?
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- C++ a class with an array of structs, without know
- How to get a fixed number of evenly spaced points
相关文章
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- Is there an existing solution for these particular
- Systematically applying a function to all fields o
- How to measure complexity of a string?
Suppose you had a tree w/ 3 nodes, all labelled the same. There are at least 3 such (not-necessarily full) trees, but all will have the same traversal arrays, no matter what order. That should answer your first question.
I was playing with the orders a bit to understand them better and here are my findings:
Now you can play with it. Having an in-order with either post-order or pre-order, you can easily reconstruct the tree because you can find the root and recursively find it always for the left/right branch. In case of having pre-order and post-order together, you can find the root and left-most children & right-most children. The problem happens in case the root has only left/right children, because you cannot tell which one it is and as such, you cannot easily reconstruct the tree.
However, as being asked, having the "full" binary tree, where every vertex has either both children or any, you don't have the problem with pre/post order combination and therefore every pair of orders will help you to reconstruct the tree. However only having one of the orders, you cannot reconstruct the tree (for example knowing only left child is not enough, you don't have information about which is the right one)
No, you can't from one list alone.
think of the postorder list:
4 5 2 3 1
both trees are possible, but we don't know which one generated the list
Assuming every element in the tree is unique, we know that preorder is build like that:
and postorder like this:
if we have two lists, preorder
1 2 4 5 3
and postorder4 5 2 3 1
, we know that1
is the root of the tree, because it is the first number of the preorder list (and the last number of the postorder list). Furthermore we know that2
must be the root of the left tree and3
the root of the right tree, because they are the first numbers after the root node which are roots of the left or right tree. With this in mind we can split the lists into this:From here you can do this algorithm recursively with the left and right tree and in the end you get this:
Since every element is unique there is only one way to build the tree and therefore you can rebuild a tree from its postorder and preorder list.
In case you have elements that are the same you can't build a unique tree, example:
from these lists you could create these two trees: