Binary Tree -- (first sequence + middle sequence) = second sequence

Source: Internet
Author: User

The first and middle traversal sequences of Binary Trees are known, and the latter are obtained.

 

First, recursively construct a binary tree and then recursively obtain the post-order sequence.

 

Ideas:

 

The first node of the first sequence is the root node of the binary tree to be constructed, and the root node of the binary tree is located in the middle sequence. The left of the root node of the central sequence column is the central sequence of the Left subtree of the root node, the middle sequence of the right subtree of the root node on the right. The root node of the first sequence is followed by the first sequence of the left and right subtree respectively. With the position of the root node in the central sequence, we can know the respective positions of the first sequence of the left and right subtree. In this way, we know the sequence of two Subtrees at the root node.

After constructing the root node, you can call the function recursively to collude with the left and right subtree of the root node.

 

The above is the binary tree restoration.

 

You can also use recursion to traverse Binary Trees in descending order.

 

The Code is as follows:

 

String. Find () returns the subscript of the search element.

String. substr (a, B) truncates B elements starting from the element of the lower Mark.

The Code is as follows:

# Include <iostream> # include <cstdio> # include <string> using namespace STD; struct node {char data; node * lchild; node * rchild ;}; node * creattree (string pre, string in) {node * root = NULL; // tree initialization if (pre. length ()> 0) {root = new node; // The Memory root required for applying the struct for the root node-> DATA = pre [0]; // The first element of the first sequence is the root node int Index = in. find (root-> data); // search for the root node location root-> lchild = creattree (pre. substr (1, index), in. substr (0, index); // Recursion Create left subtree root-> rchild = creattree (pre. substr (index + 1), in. substr (index + 1); // recursively create the right subtree} return root;} void postorder (node * root) // recursively traverse the back-order {If (root! = NULL) {postorder (root-> lchild); postorder (root-> rchild); cout <root-> data ;}} int main () {string pre_str, in_str; node * root; while (CIN> pre_str> in_str) {root = creattree (pre_str, in_str); postorder (Root); cout <Endl;} return 0 ;}

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.