HDU 1710-Binary Tree Traversals (rebuilding a Binary Tree), hdutraversals

Source: Internet
Author: User

HDU 1710-Binary Tree Traversals (rebuilding a Binary Tree), hdutraversals
Binary Tree TraversalsTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 3340 Accepted Submission (s): 1500


Problem DescriptionA binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. there are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. they are preorder, inorder and postorder. let T be a binary tree with root r and subtrees T1, T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.

 
InputThe input contains several test cases. the first line of each test case contains a single integer n (1 <= n <= 1000), the number of vertices of the binary tree. followed by two lines, respectively indicating the preorder sequence and inorder sequence. you can assume they are always correspond to a exclusive binary tree.
 
OutputFor each test case print a single line specifying the corresponding postorder sequence.
 
Sample Input
91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6
 
Sample Output
, And today, I have spent some time on the topic of Binary Trees. After half a day, I have finally come to a new idea. First of all, let's talk about rebuilding a binary tree in the first and middle order, the general idea is probably like this: because the first letter of the pre must be the root node, you need to find the root node location k in the middle order (ins) sequence, then, recursively reconstruct the left and right subtree of the root node. In this case, the left subtree and the right subtree should be treated as the root node and then represent their locations. The left subtree is easy to say that pre + 1 is its location. For the right subtree, pre + k + 1 is its position. For reconstruction from the middle and back orders, the idea is roughly the same as above. The specific writing method is a little different, in this case, we need to find the last letter (root node) in the back sequence, and then recursively reconstruct the left and right subtree, the left subtree and the right subtree are still regarded as the root node, and the position of the Left subtree is pos + k-1, however, the input parameter should be pos (because the letter we are looking for is pos [n-1]), and the right subtree is located in the second-to-last letter in the back order, the parameter is set to pos + k (not pos + k + 1. If you want to understand this, you can understand it.) In addition, this article did not write the code re-built from the back-order and Middle-order. It was previously written, click here to open the link
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#include <list>using namespace std;const int INF=1<<27;const int maxn=5100;typedef struct node{int data;node *l,*r;}*T;void rbuild(T &root,int *pre,int *ins,int n){  if(n<=0)root=NULL;  else  {  root=new node;  int k=find(ins,ins+n,pre[0])-ins;root->data=pre[0];rbuild(root->l,pre+1,ins,k);rbuild(root->r,pre+k+1,ins+k+1,n-k-1);  }}int ans[maxn],p;void last(T root){if(root){last(root->l);last(root->r);ans[p++]=root->data;}}int main(){int n,i;while(cin>>n){T root;int pre[maxn],ins[maxn];for(i=0;i<n;i++)cin>>pre[i];for(i=0;i<n;i++)cin>>ins[i];rbuild(root,pre,ins,n);p=0;last(root);for(i=0;i<p;i++)if(i!=p-1)cout<<ans[i]<<" ";    elsecout<<ans[i]<<endl;}return 0;}



Hdu 1710 Binary Tree Traversals wrong answer Solution

# Include <stdio. h>
Void build (int n, int a [], int B [], int c [])
{

Int I;
Int p;
If (n <= 0)
{
Return;
}
P = a [0];
For (I = 0; I <n; I ++)
{
If (B [I] = p)
Break;
}
Build (I, a + 1, B, c );
Build (n-i-1, a + I + 1, B + I + 1, c + I );
C [n-1] = p;
}

Int main ()
{
Int a [1001], B [1001], c [1001];
Int m;
Int I;
While (scanf ("% d", & m )! = EOF)
{
For (I = 0; I <m; I ++)
{
Scanf ("% d", & a [I]);
}
For (I = 0; I <m; I ++)
{
Scanf ("% d", & B [I]);
}
Build (m, a, B, c );
For (I = 0; I <m-1; I ++)
{
Printf ("% d", c [I]);
}
Printf ("% d \ n", c [I]);
}
Return 0;
}
N (1 <= n <= 1000), not 1 to 9


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.