Given the pre-order traversal sequence and the post-order traversal sequence, a binary tree cannot be determined!

[Conclusion]: Knowing the pre-order traversal sequence and the middle-order traversal sequence, a binary tree can be uniquely determined. The
                  known post-order traversal sequence and the middle-order traversal sequence can be uniquely determined for a binary tree
                  but the pre-order traversal sequence and the post-order are known. Traverse the sequence, it is impossible to determine a binary tree,
                  that is: a binary tree cannot be determined without an in-order traversal sequence

[Example] The following example uses pre-order traversal and middle-order traversal to determine a unique binary tree.
    Pre-order traversal: EACBDGF
    In-order traversal: ABCDEFG
1. First, find out that the root node is E according to the pre-order traversal, and then according to the in-order traversal, we can know that ABCD is the left subtree of E, and FG is the right subtree of E.

2. Then according to the pre-order of the left subtree: ACBD, middle order: ABCD, determine A as the root node, no left subtree, and the right subtree as BCD

3. The right subtree is BCD, first order: CBD, middle order: BCD, confirm that C is the root node, B is the left subtree, and the right subtree is D

4. The right subtree is GF, the first order: GF, the middle order: FG, make sure that G is the root node, there is no right subtree, and the left subtree is F

5. The final binary tree is:

       The post-order traversal sequence and the middle-order traversal sequence can uniquely determine that a binary tree is very similar to the previous one. First, determine the root node according to the last element of the post-order traversal, and then divide it into the left and right sub-trees through the middle-order traversal, and then in the sub-tree. The tree determines the root node, and so on.
 

Guess you like

Origin blog.csdn.net/weixin_43332715/article/details/114879997