Binary Tree traversals
Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4198 accepted Submission (s): 1900
Problem Description A binary is a finite set of vertices this is either empty or consists of a root R and two Disjoin T binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary-can be systematically traversed or ordered. They are preorder, inorder and Postorder. Let T is 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, the n the vertices of T2 in preorder.
In a inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root R, followed by the Ver Tices of T2 in Inorder.
In a postorder traversal of the vertices of T, we visit the vertices of T1 to postorder, then the vertices of T2 in postor Der and finally we visit R.
Now you are given the preorder sequence and inorder sequence of a certain binary. Try to find out its postorder sequence.
Input the input contains several test cases. The A single integer n (1<=n<=1000), the number of vertices of the ' binary tree ' of each test case contains . followed by two lines, respectively indicating the preorder sequence and inorder. You can assume they are always correspond-a exclusive tree.
Output for each test case print a single line specifying the corresponding postorder sequence.
Sample Input
9 1 2 4 7 3 5 8 9 6 4 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
The first order and middle order of a binary tree, let you ask for the following sequence.
Idea: The root node of the tree can be judged by the sequence of the preceding sequence, and the root node position is found in the sequence of sequences. The record root node, which is known as the Zoozi tree on the left of the root node, the right subtree, the root node of the tree is divided into two Shang trees, and the child root nodes of each Shang tree are searched; The process is not divided into the subtree So far.
AC Code:
#include <cstdio>
#include <cstring>
#define MAX 1000+10
using namespace std;
int Preorder[max];
int Inorder[max];
int Postorder[max];
void build (int n, pre, int in, int rec)
{//n current subtree nodes pre current child root node in sequence sequence start query the starting position of the root node rec array subscript
int i;< C10/>if (n <= 0) return;//subtree can not be divided for
(i = 0;; i++)
{
if (preorder[pre] = = Inorder[in+i])/Lookup child root node location
break;
}
Build (I, pre+1, in, rec);//Zuozi continue to find build
(n-i-1, pre+i+1, in+i+1, rec+i);//Right subtree continue to find
postorder[n-1+rec] = preorder[pre];//because the following sequence is the first Zuozi and then the right subtree of the notice of the change
}
int main ()
{
int n;
int I, J;
while (scanf ("%d", &n)!= EOF)
{for
(i = 0; i < n; i++)
scanf ("%d", &preorder[i]);
for (i = 0; i < n; i++)
scanf ("%d", &inorder[i]);
Build (n, 0, 0, 0);
for (i = 0; i < n; i++)/output sequential sequence
i = = 0? printf ("%d", Postorder[i]): printf ("%d", Postorder[i]);
printf ("\ n");
}
return 0;
}