【在 j********2 的大作中提到】 : Given preorder of a binary tree, generate all the possible binary trees. : Note that it is not necessarily a binary search tree.
This one is too easy, not interesting. Try this one, which is more interesting (and difficult): Given pre-order and post-order sequences of {string node_value, bool is_leaf } of a GENERAL tree, reconstruct the tree, HOWEVER, "node_value" CANNOT be used to identify a node (i.e., there could be duplicate node values).
【在 j********2 的大作中提到】 : Given preorder of a binary tree, generate all the possible binary trees. : Note that it is not necessarily a binary search tree.
b*n
8 楼
水浒:叔叔救我?
j*2
9 楼
大牛贴个代码吧
leaf
【在 g*******y 的大作中提到】 : This one is too easy, not interesting. : Try this one, which is more interesting (and difficult): : Given pre-order and post-order sequences of {string node_value, bool is_leaf : } of a GENERAL tree, reconstruct the tree, HOWEVER, "node_value" CANNOT be : used to identify a node (i.e., there could be duplicate node values).
【在 g*******y 的大作中提到】 : This one is too easy, not interesting. : Try this one, which is more interesting (and difficult): : Given pre-order and post-order sequences of {string node_value, bool is_leaf : } of a GENERAL tree, reconstruct the tree, HOWEVER, "node_value" CANNOT be : used to identify a node (i.e., there could be duplicate node values).
【在 g*******y 的大作中提到】 : This one is too easy, not interesting. : Try this one, which is more interesting (and difficult): : Given pre-order and post-order sequences of {string node_value, bool is_leaf : } of a GENERAL tree, reconstruct the tree, HOWEVER, "node_value" CANNOT be : used to identify a node (i.e., there could be duplicate node values).
The idea is to reconstruct the original tree while walking through it, using the given sequences. We use pre-order sequence to traverse down, while usin g post-order sequence to back out. The reconstructed tree will be a general tree, meaning each internal node ca n have arbitrary number of children. Also each node will have a parent point er. We need three pointers: treePtr, prePtr and postPtr that point to the curren t tree node, pre-order sequence and post-order sequence, respectively. The r oot is the first node of pre-order sequence, clearly. treePtr can be initial ized to point to root. Pseudo code: while (prePtr != end) do{ // advance prePtr prePtr = next(prePtr); // add a child to current tree node // also update treePtr to point to the newly added child treePtr = treePtr.addChild(prePtr); } while(prePtr is not leaf); do{ // traverse up the tree using post-order sequence treePtr = treePtr.parent; postPtr = next(postPtr); } while(postPtr is not leaf); }
leaf
【在 g*******y 的大作中提到】 : This one is too easy, not interesting. : Try this one, which is more interesting (and difficult): : Given pre-order and post-order sequences of {string node_value, bool is_leaf : } of a GENERAL tree, reconstruct the tree, HOWEVER, "node_value" CANNOT be : used to identify a node (i.e., there could be duplicate node values).
c*x
23 楼
just maintain two multi-set, recurse to next layer when two multiset are equal
leaf
【在 g*******y 的大作中提到】 : This one is too easy, not interesting. : Try this one, which is more interesting (and difficult): : Given pre-order and post-order sequences of {string node_value, bool is_leaf : } of a GENERAL tree, reconstruct the tree, HOWEVER, "node_value" CANNOT be : used to identify a node (i.e., there could be duplicate node values).
H*r
24 楼
听起来挺有意思,能给个example input吗?
leaf
【在 g*******y 的大作中提到】 : This one is too easy, not interesting. : Try this one, which is more interesting (and difficult): : Given pre-order and post-order sequences of {string node_value, bool is_leaf : } of a GENERAL tree, reconstruct the tree, HOWEVER, "node_value" CANNOT be : used to identify a node (i.e., there could be duplicate node values).
【在 Z*****Z 的大作中提到】 : The idea is to reconstruct the original tree while walking through it, using : the given sequences. We use pre-order sequence to traverse down, while usin : g post-order sequence to back out. : The reconstructed tree will be a general tree, meaning each internal node ca : n have arbitrary number of children. Also each node will have a parent point : er. : We need three pointers: treePtr, prePtr and postPtr that point to the curren : t tree node, pre-order sequence and post-order sequence, respectively. The r : oot is the first node of pre-order sequence, clearly. treePtr can be initial : ized to point to root.
g*y
27 楼
Great, this is the correct and best solution.
using usin ca point curren r initial
【在 Z*****Z 的大作中提到】 : The idea is to reconstruct the original tree while walking through it, using : the given sequences. We use pre-order sequence to traverse down, while usin : g post-order sequence to back out. : The reconstructed tree will be a general tree, meaning each internal node ca : n have arbitrary number of children. Also each node will have a parent point : er. : We need three pointers: treePtr, prePtr and postPtr that point to the curren : t tree node, pre-order sequence and post-order sequence, respectively. The r : oot is the first node of pre-order sequence, clearly. treePtr can be initial : ized to point to root.
Z*Z
28 楼
thank you for providing this interesting problem!
【在 g*******y 的大作中提到】 : Great, this is the correct and best solution. : : using : usin : ca : point : curren : r : initial
H*r
29 楼
不紧这个思路太清晰了,parent pointer应用的实在巧妙啊!
using usin ca point curren r initial
【在 Z*****Z 的大作中提到】 : The idea is to reconstruct the original tree while walking through it, using : the given sequences. We use pre-order sequence to traverse down, while usin : g post-order sequence to back out. : The reconstructed tree will be a general tree, meaning each internal node ca : n have arbitrary number of children. Also each node will have a parent point : er. : We need three pointers: treePtr, prePtr and postPtr that point to the curren : t tree node, pre-order sequence and post-order sequence, respectively. The r : oot is the first node of pre-order sequence, clearly. treePtr can be initial : ized to point to root.
do{ // traverse up the tree using post-order sequence treePtr = treePtr.parent; postPtr = next(postPtr); //what is the value of postPtr here? How can this make us go back to the root? } while(postPtr is not leaf); Also, is it true that (preorder, postorder) can uniquely determine a general tree? Many thanks!
using usin ca point curren r initial
【在 Z*****Z 的大作中提到】 : The idea is to reconstruct the original tree while walking through it, using : the given sequences. We use pre-order sequence to traverse down, while usin : g post-order sequence to back out. : The reconstructed tree will be a general tree, meaning each internal node ca : n have arbitrary number of children. Also each node will have a parent point : er. : We need three pointers: treePtr, prePtr and postPtr that point to the curren : t tree node, pre-order sequence and post-order sequence, respectively. The r : oot is the first node of pre-order sequence, clearly. treePtr can be initial : ized to point to root.
l*i
32 楼
Do you really need two sequences, seems one of them suffices since you have is_leaf information. Let's look at preorder sequence: let the sequence be node[0] to node[n-1] you know node[n-1] has to be a leaf, actually the last leaf if you order the leaves left to right. Now start from pos=n-1, pos-- until you hit a node[i] which is_leaft == false. This node has to be the parent of node[i+1..n-1]. So you construct such a tree node with the children i+1 to n-1. Now you can treat the tree node as a leaf, that is, you collapse the subtree rooted at node[i] into a single node. Then you can repeat the same step until the full tree is constructed. any problem with the idea?
leaf
【在 g*******y 的大作中提到】 : This one is too easy, not interesting. : Try this one, which is more interesting (and difficult): : Given pre-order and post-order sequences of {string node_value, bool is_leaf : } of a GENERAL tree, reconstruct the tree, HOWEVER, "node_value" CANNOT be : used to identify a node (i.e., there could be duplicate node values).
T*r
33 楼
what's ur result for following pre-order traversal sequence? (7 F) (3 F) (5 T) (6 T) (4 T) T/F means a node is/is not a leaf.
have the
【在 l***i 的大作中提到】 : Do you really need two sequences, seems one of them suffices since you have : is_leaf information. Let's look at preorder sequence: : let the sequence be node[0] to node[n-1] : you know node[n-1] has to be a leaf, actually the last leaf if you order the : leaves left to right. : Now start from pos=n-1, pos-- until you hit a node[i] which is_leaft == : false. This node has to be the parent of node[i+1..n-1]. So you construct : such a tree node with the children i+1 to n-1. Now you can treat the tree : node as a leaf, that is, you collapse the subtree rooted at node[i] into a : single node. Then you can repeat the same step until the full tree is
l*i
34 楼
7 | 3 | \ \ | | | 5 6 4 I found the mistake, there is another tree 7 | \ 3 4 |\ 5 6
T*r
35 楼
So you mean (7 (3 5 6 4)). What abt (7 (3 5 6) 4) or (7 (3 5) 6 4)?
【在 l***i 的大作中提到】 : 7 : | : 3 : | \ \ : | | | : 5 6 4 : I found the mistake, there is another tree : 7 : | \ : 3 4