Knowing the sequence of post-root traversal and mid-root traversal of a tree binary tree, write its pre-root sequence or knowing the sequence of pre-root traversal and mid-root traversal of a tree binary tree, write its post-root sequence

Question 1:

It is known that the sequences of post-root traversal and mid-root traversal of a binary tree are: ACDBGIHFE and ABCDEFGHI, respectively,

Please draw this binary tree and write the sequence of its predecessors traversed

Root traversal sequence: EBADCFHGI

Problem solving ideas:

Let's find the root first

  Back Root: Left     and Right Root                                          Middle Heel: Left   Root   Right 

 ACDB    GIHF   E                                           ABCD   E    FGHI

We first look for the back root, this root must be behind this traversal, we can determine that E is the root

Then we find the position of E in the middle root traversal, so the left side of E is the left index node and the right side is the right index node

Now that the top number E is determined, then who is the root on the left exponent of E, and who is behind and who is the root: it is B

Then look at the root traversal, who is on the left of B and who is the left index: it is A; so:

Now only C and D are not drawn on the left index,

Fancy the root traversal to get: C and D are on the right side of B, look at the back root traversal and know that D is behind, so D is the root,

In the middle root traversal, C is to the left of D, so C is the left index of D;

So: take E as the left index of the root node to finish drawing

Next, look at the right index with E as the root node, first find the root node in the back root traversal, and who is behind who is the root node: it is F

Then find the F position in the middle root traversal, the left side of F is finished, so there is no left index, and then look at the right side: GHI

Go back to the post-root traversal to see GHI, and know that H is the root node, so:

Now only G and I are left unpainted, so go back to the middle root traversal and see that G is on the left of H and I is on the right of H, so:

If you want to know if the painting is correct, bring it into the question stem and see if his back root traversal and middle root traversal are correct.

Question two:

It is known that the sequence of pre-root traversal and mid-root traversal of a binary tree are: ABDGHCEFI and GDHBAECIF, respectively,

Please draw this binary tree and write the sequence of its post-root passes

The constructed binary tree is as follows

Post root traversal sequence: GHDBEIFCA

Problem- solving ideas:
first find the root

Tip root:  Root    left and right    Middle root                           : Left root Right        

        A    BDGH    CEFI                          GDHB   A   ECIF

Look at the first root traversal and know that A is the root, and then find the position of A in the middle root traversal

So we know that GDHB is on the left of A and ECIF is on the right of A

Who is the root of GDHB? Back to the first root traversal, who is in front and who is the root, we know that it is B

Look at the middle root again, GDH is on the left of B, that is the left index of B

Who is the root of the GDH species? Look at the root, D is in front, then D is the root

Then go to the position of D in the middle root traversal, and know that the left is G and the right is H

Now that all left indices with A as the root node have been found, the right index is ECIF

Going back to the first root traversal, who is the root of the four who are in front, is C

Then go to the middle root traversal to find the position of C, E is on the left side of C (left index), IF is on the right side of C (right index)

Then IF who is the root? Go to the root traversal to find: F is first, F is the root

In the middle root traversal, I is to the left of F, so I is the left index of F 

Guess you like

Origin blog.csdn.net/rej177/article/details/124060415