03-树3 Tree Traversals Again
来源:互联网 发布:那的中超数据比较全 编辑:程序博客网 时间:2024/05/10 12:14
03-树3 Tree Traversals Again(25 分)
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 6PopPop
Sample Output:
3 4 2 6 5 1
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- PTA 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again (25分)
- 03-树3 Tree Traversals Again (25分)
- 03-树3 Tree Traversals Again (25分)
- 03-树3 Tree Traversals Again (25分)
- 03-树3 Tree Traversals Again (25分)
- 03-树3 Tree Traversals Again (25分)
- 维基百科遭土耳其政府封锁,创始人高声呼吁人权
- 高德董振宁:地图行业格局已定 将更关注网约车与共享单车
- GMIC 来了,看科技界的群雄逐鹿!
- 微信iOS版打赏功能被关闭!都怪苹果
- 连接世界不只是说说,这“三大杀器”帮助扎克伯格圆梦丨F8 2017
- 03-树3 Tree Traversals Again
- 优先队列
- 《大型网站技术架构:核心原理与案例分析》——大型网站架构演化过程
- HTML关于div的一些设置
- 利用JS实现动态进度条效果
- C++拷贝控制与资源管理
- Activity与Activity之间简单的数据传递
- 将List<Map>/Map中的key转换为驼峰规则
- hdu 5137 How Many Maos Does the Guanxi Worth(Dijkstra)