Sequential traversal of pre-order sequence

Source: Internet
Author: User

A Hulu pen Test (how I wish yesterday once more)

Suppose there is a tree, long below this look, its pre-sequence traversal, the middle sequence traversal, the subsequent traversal is easy to know.

Preorder:gdafemhz

Inorder:adefghmz

Postorder:aefdhzmg

Now, assuming that only the pre-order and middle-order traversal are known, how do we get the post-sequence traversal? For example, it is known that the pre-order traversal of a tree is "Gdafemhz", while the middle sequence traversal is "ADEFGHMZ" how should the subsequent traversal be?

In the first step, root is the simplest, and the first node of the pre-sequence traversal is root.

The second step, continue to observe the pre-sequence traversal Gdafemhz, in addition to know that G is root, the remaining nodes must be root of the left and right subtree, can not find more information.

The third step is to observe the middle sequence traversal adefghmz. The adef on the left side of the root node is necessarily the left subtree of root, and the hmz on the right of G must be the right subtree of root.

Fourth step, observe the left subtree adef, the root node of Zuozi must be the root of the tree leftchild. In the pre-sequence traversal, the root leftchild of the tree is located after root, so the root node of the Zuozi is D.

The fifth step, the same truth, root in the right subtree node HMZ root node can also be obtained through the pre-sequence traversal. In the pre-sequence traversal, the right subtree must be traversed before all the left subtree nodes of root and root are traversed, and the first node of the right subtree is the root node of the right subtree.

How do you know where is the dividing line of the Saozi right subtree in the pre-sequence traversal? The number of nodes to be traversed through the middle sequence.

In the last middle-order traversal, the left side of root is a, D, E, F, so there are 4 nodes on the left side of the root. So in the first sequence traversal, is bound to be the 1th is G, 2nd to 5th by a, D, E, F process, 6th is the root node of the right subtree, is M.

The sixth step, observed that the above process is recursive. First find the root node of the current tree, then divide it into left dial hand tree, right subtree, and then go to the left subtree to repeat the above procedure, and then go to the right subtree to repeat the above procedure. Finally, you can restore a tree.

The seventh step, in fact, if only asked to write a follow-up traversal, or even do not specifically occupy space to save the restored tree. Just make a little change to the sixth step and you'll be able to implement the requirements. You only need to change the recursive process of step sixth to the following:

1 Determine the root, determine the left subtree, and determine the right subtree.

2 recursion in record.

3 recursively in the right subtree.

4 Prints the current root.

Reprint http://blog.csdn.net/feliciafay/article/details/6816871

Sequential traversal of pre-order sequence

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.