Binary Tree Series (ii): The Central and post-order traversal sequences are known, and the first-order traversal sequences are obtained.

Source: Internet
Author: User
We have already introduced the rules for three traversal methods. For your convenience, we will re-introduce them here:

1. Sequential Traversal

(1) access the root node;

(2) traverse the left subtree in sequence;

(3) traverse the right subtree in sequence.

2. Sequential Traversal

(1) traverse the left subtree in the middle order;

(2) access the root node;

(3) traverse the right subtree in ascending order.

3. Post-order traversal

(1) traverse the left subtree in descending order;

(2) traverse the right subtree in descending order;

(3) Access the root node.

Knowing the three traversal rules of a binary tree, we can find the third traversal sequence as long as there is a middle-order traversal sequence and any previous or earlier traversal sequence, today we are studying the known and post-order traversal sequences to find the first-order traversal sequence.

It is known that the central traversal sequence of this binary tree is: D-B-G-E-A-C-F, and the post-order traversal sequence is: D-g-e-B-f-C-.

Next, we can find the first sequential traversal sequence of the binary tree. The specific steps are as follows:

Step 1: Find the root node first. Based on the Post-order traversal rules, we can see that root is the last node of the Post-order traversal sequence. Therefore, the root node of this binary tree is.

Step 2: Find the left subtree and right subtree of the root, this we still find from the traversal sequence in the middle order, the D-B-G-E located on the left of the root node A is the left subtree of the root, the C-F on the right side of a is the right subtree.

Step 3: Request the left child leftchild and right child rightchild of the root user. leftchild indicates the root node of the Left subtree and rightchild indicates the root node of the right subtree. We can find that the order of the Left subtree D-B-E-G in the Post-sequential traversal sequence is D-G-E-B, because the post-sequential traversal of the last access to the root node, so B is the root node of the Left subtree, that is, leftchild with B as root; similarly, rightchild with root is C.

Step 4: follow the steps above to find the left and right subtree of B, and the left and right subtree of C, and then find the root nodes of the left and right subtree respectively. Similarly, as long as the root node and Its leftchild and rightchild are obtained, the rest of the process is recursive. Finally, we can restore the entire binary tree.

The binary tree obtained based on the above steps is shown in:

Finally, we can directly obtain the first traversal sequence of the binary tree based on the first traversal rule: a-B-d-e-g-C-f.

By contacting the previous blog, we can see that the methods for finding another sequence are similar, whether you know the middle sequence and the first sequence or the middle sequence and the latter, however, from the left and right subtree of root, we can see that the central sequence must be known conditions. At present, the middle order cannot be obtained based on the first and last order at the current level. I am not sure whether I can assert that I can or cannot. Which of the following experts knows the result and hopes to give it a try.

Binary Tree Series (ii): The Central and post-order traversal sequences are known, and the first-order traversal sequences are obtained.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.