原题:
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
D / \ / \ B E / \ \ / \ \ A C G / / F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree).
For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input Specification
The input file will contain one or more test cases. Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)Input is terminated by end of file.
Output Specification
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFG BCAD CBAD
Sample Output
ACBFGED CDAB
我的代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 using namespace std; 6 7 8 9 10 11 char preorder[30]; 12 char inorder[30]; 13 queue<char> post; 14 15 struct tree 16 { 17 char date; 18 tree * lchild; 19 tree * rchild; 20 //tree():date('\0'),lchild(NULL),rchild(NULL){} 21 }; 22 23 24 25 tree * buildtree(char pre[], char in[], int n) 26 { 27 if (n == 0) 28 return NULL; 29 else 30 { 31 int Lnum, Rnum; 32 tree *p = new tree(); 33 p->date = pre[0]; 34 for (int i = 0; i < n; i++) 35 { 36 if (in[i] == pre[0]) 37 { 38 Lnum = i; 39 Rnum = n - 1 - i; 40 break; 41 } 42 } 43 p->lchild = buildtree(pre + 1, in, Lnum); 44 p->rchild = buildtree(pre + Lnum + 1, in + Lnum + 1, Rnum); 45 return p; 46 } 47 48 } 49 50 51 void postorder(tree *pR) 52 { 53 if (pR == NULL) 54 { 55 return; 56 } 57 postorder( pR->lchild); 58 postorder( pR->rchild); 59 post.push(pR->date); 60 return; 61 } 62 63 void deletetree (tree * p) 64 { 65 if (p == NULL) 66 { 67 return; 68 } 69 deletetree(p->lchild); 70 deletetree(p->rchild); 71 delete(p); 72 return; 73 } 74 75 int main() 76 { 77 while (cin >> preorder) 78 { 79 80 cin >> inorder; 81 tree t; 82 tree *pRoot=buildtree(preorder, inorder, strlen(inorder)); 83 postorder(pRoot); 84 deletetree(pRoot); 85 while (1) 86 { 87 if (!post.empty()) 88 { 89 cout << post.front(); 90 post.pop(); 91 } 92 else 93 { 94 cout << endl; 95 break; 96 } 97 } 98 } 99 100 101 return 0; 102 }