Problem Description
A 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.
Input
The 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.
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
Note: The pre-and mid-order traversal of a known binary tree can uniquely determine the post-order traversal of the binary tree. However, if you know the pre-and post-order, it is impossible to find the middle-order traversal.
Algorithm:
The root node of the left and right sub-trees can be determined by the first element traversed in the forward order, and the left and right sub-trees can be further determined by referring to the middle order traversal,
The right subtree element. Recursively refer to two traversal sequences to construct a binary tree.
Returns the result of post-order traversal based on the pre-order and Middle-order results
Tree traversal: provides you with the first and middle traversal results of a tree, so that you can find the result of the subsequent sequential traversal and use recursion.
Divide the two arrays into three parts: parent node, left subtree, and right subtree. Put the parent node in the array. Repeat this step until a new tree is rebuilt.
In this case, the elements in the array are just the order of post-order traversal.
Key points:
The central traversal feature is that the Left subtree is first traversed, followed by the root node, and then the right subtree is traversed. In this way, the root node separates the Left and Right Subtrees. The feature of pre-order traversal is to first access the root node, so that the result of pre-order traversal provides the root node information, and the middle-order traversal provides the Left and Right sub-tree information, so as to re-build a binary tree.
[Note]
In the first sort, the first element is the root, and then compare the position of the central sort, the number of elements in the left and right subtree numleft can be determined, numright: In the first order, there is a root, then the elements of numleft left subtree, and finally the elements of numright right subtree.
This process is to find a root from the inorder array, and then determine from the position of the preorder array whether to change to the left son or the right son. This keeps repeating and knows that a complete number is created.
#include <stdio.h>#include <stdlib.h>const int MAX = 1000 + 10;int n,in[MAX],pre[MAX];typedef struct BITree{ int data,index; BITree *Left,*Right;}BiTree,*Tree;void DFS(Tree &root,int index){ if(root == NULL){ root = (Tree)malloc(sizeof(BiTree)); root->data = in[index]; root->index = index; root->Left = NULL; root->Right = NULL; }else { if(index < root->index) DFS(root->Left,index); else DFS(root->Right,index); }}void CreateTree(Tree &root){ int i,j,index; root = (Tree)malloc(sizeof(BiTree)); for(i = 1;i <= n;i++) if(in[i] == pre[1]) { root->data = pre[1]; root->index = i; root->Left = NULL; root->Right = NULL; break; } index = i; for(i = 2;i <= n;i++) for(j = 1;j <= n;j++) if(in[j] == pre[i]) { if(j < index) DFS(root->Left,j); else DFS(root->Right,j); break; }}void PostOrder(Tree root,int x){ if(root == NULL) return ; PostOrder(root->Left,x+1); PostOrder(root->Right,x+1); if(x == 0) printf("%d",root->data); else printf("%d ",root->data);}int main(){ int i; while(scanf("%d",&n)!=EOF) { Tree root; for(i = 1;i <= n;i++) scanf("%d",&pre[i]); for(i = 1;i <= n;i++) scanf("%d",&in[i]); CreateTree(root); PostOrder(root,0); printf("\n"); } return 0;}
# Include <iostream> # include <cstdio> using namespace std; const int MAX = 1000 + 10; typedef struct BITree {int data; BITree * Left, * Right; BITree () {Left = NULL; Right = NULL ;}} * BiTree; int pre [MAX], in [MAX]; void BuildTree (BiTree & root, int len, int pst, int ped, int inst, int ined) {int I, left_len = 0; if (len <= 0) return; // recursive termination condition root = new BITree; root-> data = pre [pst]; for (I = inst; I <= ined; I ++) if (in [I] = pre [pst]) {left_len = I-inst; break;} BuildTree (root-> Left, left_len, pst + 1, pst + left_len, inst, i-1); BuildTree (root-> Right, len-left_len-1, pst + left_len + 1, ped, I + 1, ined);} void PostTravel (BITree * root) {if (root) {PostTravel (root-> Left); PostTravel (root-> Right); printf ("% d", root-> data) ;}} int main () {int I, n; BiTree root; while (scanf ("% d", & n )! = EOF) {for (I = 1; I <= n; I ++) scanf ("% d", & pre [I]); for (I = 1; I <= n; I ++) scanf ("% d", & in [I]); BuildTree (root, n, 1, n, 1, n ); postTravel (root-> Left); PostTravel (root-> Right); printf ("% d \ n", root-> data);} return 0 ;}