1020. Tree traversals (25)-pat question 1385: rebuilding a binary tree

Source: Internet
Author: User
1020. Tree traversals (25) Time Limit 400 ms memory limit 32000 kb code length limit 16000 B discriminant program standard author Chen, Yue

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input specification:

Each input file contains one test case. for each case, the first line gives a positive integer N (<= 30), the total number of nodes in the binary tree. the second line gives the postorder sequence and the third line gives the inorder sequence. all the numbers
In a line are separated by a space.

Output specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. all the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample input:

72 3 1 5 7 6 41 2 3 4 5 6 7

Sample output:

4 1 6 3 5 7 2
Recommendation index :※※
Source: http://pat.zju.edu.cn/contests/pat-a-practise/1020
1. One method is to construct a tree using the back-order and Middle-order before BFs.
2. Another method is to calculate the left and right son nodes of each node without constructing a number.
A. left son: calculates the number of nodes with the right sub-number of the current node in the middle order, and then calculates the number of nodes with the right sub-tree in the current node position minus the number of nodes with the right sub-tree, position (in fact, the central order is to determine the size of the right subtree of the current node ).
rsize=right_size(root);left=hash_post[root]-rsize-1;
B. The right son traverses the previous node of the current node in the descending order. Right = hash_post [root]-1;
#include<iostream>#include<queue>#include<string.h>using namespace std;int *post,*visited;const int N=31;int hash_post[N],hash_in[N];int n;int right_size(int root){int i=0,size=0;for(i=hash_in[root]+1;i<=n-1;i++){if(visited[i]==false)size++;elsebreak;}return size;}int main(){    cin>>n;    post=new int[n];visited=new int [n];    int i,tmp;    for(i=0;i<n;i++){        cin>>post[i];hash_post[post[i]]=i;}    for(i=0;i<n;i++){cin>>tmp;hash_in[tmp]=i;}memset(visited,0,n*sizeof(int));queue<int> q;q.push(post[n-1]);int right=n-1;cout<<post[n-1];visited[hash_in[post[n-1]]]=true;while(!q.empty()){int root=q.front();int rsize=right_size(root);int left=hash_post[root]-rsize-1;//handle left sub tree rootif(left>=0&&visited[hash_in[post[left]]]==false){int left_root=post[left];cout<<" "<<left_root;visited[hash_in[left_root]]=true;q.push(left_root);}int right=hash_post[root]-1;//handle right sub tree rootif(right>=0&&visited[hash_in[post[right]]]==false){int right_root=post[right];cout<<" "<<right_root;visited[hash_in[right_root]]=true;q.push(right_root);}q.pop();}    return 0;}

9 degrees:

Topic 1385: rebuilding a binary tree
Description:

Enter the result of the forward and middle traversal of a binary tree. Create a New Binary Tree. Assume that the input results do not contain repeated numbers. For example, input pre-order traversal sequences {1, 2, 4, 7, 3, 5, 6, 8} and Middle-order traversal sequences {4, 7, 2, 1, 5, 3, 8, 6 }, then, the binary tree is rebuilt and its post-sequential traversal sequence is output.

Input:

The input may contain multiple test examples. For each test case,

The first input behavior is an integer N (1 <=n <= 1000): represents the number of nodes in a binary tree.

The second line of the input contains N integers (the range of each element a is (1 <= A <= 1000), which indicates the pre-order traversal sequence of the binary tree.

The third line of the input contains N integers (where the range of each element a is (1 <= A <= 1000): indicates the ordinal traversal sequence of the binary tree.

Output:

Output a line for each test case:

If the Pre-and mid-order traversal sequences given in the question can constitute a binary tree, N integers are output, representing the post-order traversal sequence of the binary tree. Each element is followed by spaces.

If the pre-order and mid-order traversal sequences given in the question cannot constitute a binary tree, "no" is output ".

Sample input:
81 2 4 7 3 5 6 84 7 2 1 5 3 8 681 2 4 7 3 5 6 84 1 2 7 5 3 8 6
Sample output:
7 4 2 5 8 6 3 1 No

Recommendation index :※※

Source: http://ac.jobdu.com/problem.php? PID = 1, 1385
This is the basic structure of the post-order based on the pre-order and Middle-order.
#include<iostream>using namespace std;typedef struct node{    int val;    node *left;    node *right;}node;bool flag;node * rebuild(int *pre,int *in ,int length){    node *root=new node();    root->val=pre[0];root->left=NULL;root->right=NULL;    int index;    for(index=0;index<length;index++)        if(in[index]==root->val)            break;if(index>=length){flag=true;return NULL;}if(index>0)root->left=rebuild(pre+1,in,index);if(length-index-1>0)root->right=rebuild(pre+index+1,in+index+1,length-index-1);return root;}void print_post(node* root){    if(root!=NULL){        print_post(root->left);        print_post(root->right);cout<<root->val<<" ";}}int main(){    int n,i;    while( cin>>n){flag=false;int *pre=new int[n];int *in=new int[n];for(i=0;i<n;i++)cin>>pre[i];for(i=0;i<n;i++)cin>>in[i];node *root;root=rebuild(pre,in,n);if(flag==true)cout<<"No"<<endl;else{print_post(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.