It is known that binary tree pre-order/post-order traversal and Middle-order traversal are used to find post-order/pre-order traversal.

Source: Internet
Author: User
First, let's take a look at the features of forward, middle, and backward traversal:
Forward traversal:
1. Access the root node
2. traverse the left subtree in the forward order
3. traverse the right subtree in ascending order
In-order traversal:
1. traverse the left subtree in the middle order
2. Access the root node
3. traverse the right subtree in the middle order
Post-order traversal:
1. traverse the left subtree in descending order
2. traverse the right subtree in descending order
3. Access the root node

Okay. Let's talk about how to use the forward and middle traversal to find the back-order traversal.
Assume that the first traversal is adbgcefh, and the middle traversal is dgbaechf.
In the forward traversal mode, the root node is accessed first and then the child tree is accessed. In the middle traversal mode, the left child tree is accessed first and then the root node is accessed.
Then, obtain the first order of A, and then find the position of a in the middle order traversal to obtain the dgb a echf.
Then we know that DGB is the left subtree, echf is the right subtree, because the number must be consistent.
Therefore, the corresponding dbg in the forward order is the left subtree, cefh is the right subtree.

Then it becomes a recursive process. The specific code is as follows:

# Include <iostream> # include <string> using namespace STD; int find (const string & STR, char c) {for (INT I = 0; I <Str. size (); ++ I) if (C = STR [I]) return I; Return-1;} bool premid (const string & Pre, const string & Mid) {If (pre. size () = 0) return false; If (pre. size () = 1) {cout <PRE; return true;} // The root node is the first element int K = find (MID, pre [0]); string pretmp = pre. substr (1, k); string midtmp = mid. substr (0, k); premid (pretmp, midtmp); pretmp = pre. substr (k + 1, pre. size ()-K-1); midtmp = mid. substr (k + 1, mid. size ()-K-1); premid (pretmp, midtmp); // returns the value of cout for the final output node in the descending order traversal <PRE [0];} int main () {string pre, mid; while (CIN> PRE> mid) {premid (PRE, mid); cout <Endl ;}}

However, it is known that the process of post-order traversal is similar to that of the middle-order traversal, but the post-order traversal is the final access to the root node.
Therefore, you need to start searching from the back. For example, in the above example, the post-order traversal is gbdehfca, and the middle-order traversal is dgbaechf.
The last element in the Post-order traversal is the root node, A, and then searches for the position of a in the middle order.
Divides the sequential traversal into dgb a echf, because the number of nodes must correspond
The post-order traversal is divided into GBD ehfc A, GBD is the left subtree, and ehfc is the right subtree, which can be calculated recursively.
Some additional code already exists, so I will not repeat it here. The specific code is as follows:

Bool backmid (const string & back, const string & Mid) {If (back. size () = 0) return false; If (back. size () = 1) {cout <back; return true;} // The root node is the last element int K = find (MID, back [back. size ()-1]); // returns the value of the output node cout before traversing in the forward order. <back [back. size ()-1]; string backtmp = back. substr (0, k); string midtmp = mid. substr (0, k); backmid (backtmp, midtmp); backtmp = back. substr (K, back. size ()-K-1); midtmp = mid. substr (k + 1, mid. size ()-K-1); backmid (backtmp, midtmp );}

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.