1086. Tree Traversals Again (25)
来源:互联网 发布:研究生课程表软件 编辑:程序博客网 时间:2024/05/21 07:01
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:6Push 1Push 2Push 3PopPopPush 4PopPopPush 5Push 6PopPopSample Output:
3 4 2 6 5 1
#include<cstdio>#include<cstdlib>#include<cstring>#define Maxn 31typedef struct{int parent;int leftchild;int rightchild;}Node;Node tree[Maxn];bool visited[Maxn];int N;int visitednum;int findnovisitedparent(int thisparent);void postorder(int rootnode);int main(void){scanf("%d\n",&N);int i;int total = 2*N;char operation[5];int index;int thisparent = 0;bool root = false;int rootnode = 0;for(i=0;i<total;++i){scanf("%s",operation);if(strstr(operation,"Push") != NULL){scanf("%d",&index);if(!root){rootnode = index;tree[index].parent = -1;tree[index].leftchild = -1;tree[index].rightchild = -1;thisparent = index;root = true;continue;}else{tree[index].parent = thisparent;tree[index].leftchild = -1;tree[index].rightchild = -1;if(tree[thisparent].leftchild == -1){tree[thisparent].leftchild = index;}else{tree[thisparent].rightchild = index;}thisparent = index;}}if(strstr(operation,"Pop") != NULL){if(!visited[thisparent]){visited[thisparent] = true;}else{thisparent = findnovisitedparent(thisparent);visited[thisparent] = true;}}}postorder(rootnode);system("pause");return 0;}int findnovisitedparent(int thisparent){int temp = tree[thisparent].parent;if(!visited[temp])return temp;elsereturn findnovisitedparent(temp);}void postorder(int rootnode){if(tree[rootnode].leftchild != -1)postorder(tree[rootnode].leftchild);if(tree[rootnode].rightchild != -1)postorder(tree[rootnode].rightchild);printf("%d",rootnode);++visitednum;if(visitednum != N)printf(" ");}
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- Android 基础学-4 Android系统提供
- 兔子-(PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recommended: 'CGP'
- poj 1018 Communication System-dp+离散化
- DedeCMS 友情链接按分类网站类型调用的方法
- 滑动门JS
- 1086. Tree Traversals Again (25)
- WEB java(SSH)日记(2)
- 欢迎使用CSDN-markdown编辑器
- Android应用去掉标题栏(Android Studio)
- Valid Sudoku
- [Sort]冒泡排序
- 如何将GPS数据转换成KML数据?使用JAK的例子
- crontab,linux中创建定时任务与取消
- 命令行快速跳转/编辑神器fasd