1020. Tree Traversals (25)
时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:7 2 3 1 5 7 6 4 1 2 3 4 5 6 7Sample Output:4 1 6 3 5 7 2N个结点后序中序reaDlnAndCLEAR(int*postorder, int N, int*inorder,int*leftindex,int*rightindex)读入后序、中序 、并初始化假如在中序的对应序号N的左右都没有结点设为-1;BinaryTree(int*postorder, int*roOt, int*inorder, int*leftindex, int*rightindex,int Star,int END)DFS深度优先搜索,后序的*roOt不断的减少,在中序中找到*roOt,看在此次的中序是否有右子树,有先右边,再看是否有左子树,有则左边(后序 左-右-中,所以显然每次调用此函数
如果 左-nowroot-右 ,那么(*root)=nowroot-1是nowroot的右子,且当nowroot的右子数调用完此函数后,此时(*root)才是nowroot的左子;如果 nowroot-右,那么(*root)=nowroot-1是nowroot的右子。如果 左-nowroot (*root)=nowroot-1是nowroot的左子)
GetIT(int*postorder, int N, int*inorder, int*leftindex, int*rightindex) BFS广度优先搜索,这里我没有用queue,直接那inorder来充当queue的角色;
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
7月29日 22:59 | 答案正确 | 25 | 1020 | C++ (g++ 4.7.2) | 1 | 308 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 300 | 15/15 |
1 | 答案正确 | 1 | 308 | 1/1 |
2 | 答案正确 | 1 | 252 | 2/2 |
3 | 答案正确 | 1 | 308 | 2/2 |
4 | 答案正确 | 1 | 180 | 2/2 |
5 | 答案正确 | 1 | 248 | 3/3 |
#include<iostream> using namespace std; void reaDlnAndCLEAR(int*postorder, int N, int*inorder,int*leftindex,int*rightindex) { int index; for (index = 0; index < N; index++) cin >> postorder[index]; for (index = 0; index < N; index++) { cin >> inorder[index]; leftindex[index] = -1; rightindex[index] = -1; } } void BinaryTree(int*postorder, int*roOt, int*inorder, int*leftindex, int*rightindex,int Star,int END) { int index,now,nowroot; bool Flag = true; now = postorder[(*roOt)]; nowroot = (*roOt)--; for (index = Star; index < END&&Flag; index++) if (now == inorder[index]) { Flag = false; if (index !=END - 1) { rightindex[nowroot] = (*roOt); BinaryTree(postorder, roOt, inorder, leftindex, rightindex, index+1, END); } if (index != Star) { leftindex[nowroot] = (*roOt); BinaryTree(postorder, roOt, inorder, leftindex, rightindex, Star, index); } } } void GetIT(int*postorder, int N, int*inorder, int*leftindex, int*rightindex) { int index,frontIndex,t; frontIndex = 0; index = 1; inorder[0] = N-1; while (index < N) { for (t = index; frontIndex < index; frontIndex++) { if (leftindex[inorder[frontIndex]] != -1) { inorder[t++] = leftindex[inorder[frontIndex]]; } if (rightindex[inorder[frontIndex]] != -1) { inorder[t++] = rightindex[inorder[frontIndex]]; } } frontIndex = index; index = t; } } void Display(int*postorder, int N, int*inorder) { int index = 0; for (; index < N - 1; index++) cout << postorder[inorder[index]] << " "; cout << postorder[inorder[index]] << endl; } int main() { int N; int root; int *postorder; int *inorder; int*leftindex; int*rightindex; cin >> N; postorder = new int[N]; inorder = new int[N]; leftindex = new int[N]; rightindex = new int[N]; root = N-1; reaDlnAndCLEAR(postorder, N, inorder, leftindex, rightindex); BinaryTree(postorder,&root,inorder,leftindex, rightindex,0,N); GetIT(postorder, N, inorder, leftindex, rightindex); Display(postorder,N,inorder); system("pause"); delete[]rightindex; delete[]leftindex; delete[]postorder; delete[]inorder; return 0; }