1. 程式人生 > >HDU 1710 Binary Tree Traversals

HDU 1710 Binary Tree Traversals

 

Binary Tree Traversals

  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

解題思路:
  本題給二叉樹的結點數量,之後第一行給出二叉樹的先序遍歷,第二行給出二叉樹的中序遍歷,根據前序遍歷的性質,先序遍歷的首位為根結點,以該點為根結點建樹,從中序遍歷中尋找該點,假設找到新的位置為k,則從中序遍歷首位到k-1,為左子樹中序遍歷的範圍,從k+1到中序遍歷末位為右子樹中序範圍,,這樣我們就可以得知左子樹長度,右子樹長度,進而獲得左子樹與右子樹的前序遍歷。對獲得的左子樹右子樹的前序遍歷中序遍歷進行同樣的操作,便可以建立該樹。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef int typeData;
 4 const int maxn = 1100;
 5 int pre[maxn];  //記錄前序遍歷
 6 int in[maxn];   //記錄中序遍歷
 7 struct node{
 8     typeData data; 
 9     node* leftChild;
10     node* rightChild;
11     node(){
12         leftChild = NULL;
13         rightChild = NULL;
14     }
15 };
16 
17 void postorder(node *root, node *flag){ //輸出後序遍歷
18     if(root == NULL)
19         return;
20     postorder(root->leftChild, flag);
21     postorder(root->rightChild, flag);
22 
23     printf("%d", root->data);
24     if(root != flag)
25         printf(" ");
26 }
27 //傳入先序遍歷起始位置,中序遍歷起始位置
28 node* create(int preL, int preR, int inL, int inR){
29     if(preL > preR) //如果前序遍歷中沒有數值返回NULL
30         return NULL;
31     node* root = new node();    //建立新結點,其權值為,當前先序遍歷的首位,即當前樹根結點
32     root->data = pre[preL];
33     int k;  //k記錄根結點在中序遍歷中的位置
34     for(k = inL; k < inR; k++){ //在中序遍歷中尋找根結點位置
35         if(pre[preL] == in[k])
36             break;
37     }
38     int numLeft = k - inL;  //計算左子樹長度
39     root->leftChild = create(preL + 1, preL + numLeft, inL, k - 1); //遞迴建立左子樹
40     //先序遍歷中左子樹區域為根結點的下一位到左子樹起始點加左子樹長度
41     //中序遍歷中左子樹區域為中序遍歷首位到根結點之前
42     root->rightChild = create(preL + numLeft + 1, preR, k + 1, inR);    //遞迴建立右子樹
43     //先序遍歷中右子樹區域為左子樹末尾的下一位到先序遍歷末位
44     //中序遍歷中右子樹區域為根結點後一位到中序遍歷末位
45     return root;
46 }
47 int main()
48 {
49     int n;
50     while(cin >> n){
51         memset(pre, 0, sizeof(pre));
52         memset(in, 0, sizeof(in));
53         for(int i = 0; i < n; i++){
54             scanf("%d", &pre[i]);   //輸入先序遍歷
55         }
56         for(int i = 0; i < n; i++){ //輸入中序遍歷
57             scanf("%d", &in[i]);
58         }
59         node* root = create(0, n - 1, 0, n - 1);    //建樹
60         postorder(root, root);  //輸出後序遍歷
61         printf("\n");
62     }
63 }