1086. Tree Traversals Again (25)
时间限制200 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:6 Push 1 Push 2 Push 3 Pop Pop Push 4 Pop Pop Push 5 Push 6 Pop PopSample Output:3 4 2 6 5 1和http://blog.csdn.net/u014646950/article/details/47137173差不多;左边连接的是已知后序,中序,求水平层遍历;
而这一题入栈push的顺序是先序,而pop出来的顺序是中序,这里要求的是后序;
先序 中-左-右
中序 左-中-右
先序的头为当前子树的根,接着在中序中找到根在中序的位置,可以划分为【左子树】和【右子树】然后进行继续如此;知道左右子树都不能划分了
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月17日 23:13 | 答案正确 | 25 | 1086 | C++ (g++ 4.7.2) | 1 | 308 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 300 | 12/12 |
1 | 答案正确 | 1 | 308 | 4/4 |
2 | 答案正确 | 1 | 180 | 4/4 |
3 | 答案正确 | 1 | 300 | 1/1 |
4 | 答案正确 | 1 | 180 | 4/4 |
#include<iostream> #include<vector> #include<stack> using namespace std; void createT(vector<int>*pre, vector<int>*inor, vector<pair<int, int>>*Tree, int starindex, int endindex, int rootindex) { int index = starindex; int root = (*pre)[rootindex]; bool Flag = true; for (; index <= endindex&&Flag; index++) if( (*inor)[index]==root) { if (starindex < index) { (*Tree)[root].first = (*pre)[rootindex + 1]; createT(pre, inor, Tree, starindex, index-1, rootindex+1); } if (index < endindex) { (*Tree)[root].second = (*pre)[index-starindex+rootindex+1]; createT(pre, inor, Tree, index + 1, endindex, index - starindex + rootindex+1); } Flag = false; } } void rightFirstDFS(vector<pair<int, int>>*Tree, int root,int *firstflag) { if ((*Tree)[root].first) rightFirstDFS(Tree, (*Tree)[root].first,firstflag); if ((*Tree)[root].second) rightFirstDFS(Tree, (*Tree)[root].second ,firstflag); if (*firstflag == 0) { cout << root; *firstflag = 1; } else cout << " " << root; } int main() { int N; int node; char order[5]; cin >> N; vector<pair<int,int>>Tree(N+1); vector<int>pre; vector<int>inor; stack<int>sta; int index = 2 * N; while (index--) { cin >> order; switch (order[1]) { case 'u': cin>>node; pre.push_back(node); sta.push(node); break; case 'o': inor.push_back(sta.top()); sta.pop(); break; } } createT(&pre, &inor, &Tree, 0, N - 1, 0); node = 0; rightFirstDFS(&Tree, pre[0],&node); cout << endl; system("pause"); return 0; }