03-树2. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 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
Pop

Sample Output:

3 4 2 6 5 1

这题要做的是树的中缀转后缀,一开始思路有问题,没想明白,只过了两组数据,后来静下来好好想发现也不难。

题目中给出了中缀遍历树的过程,根据题目给出的过程用栈模拟建立树,然后后缀输出。

 1 #include <iostream>
 2 #include <stack>
 3 using namespace std;
 4 
 5 struct node //树节点
 6 {
 7        int left;
 8        int right;
 9        node ()//结点初始化左右儿子为-1,表示左右结点都不存在
10        {
11             left=-1;
12             right=-1;
13        }
14 };
15 node no[50];
16 
17 stack<int> s;//用栈模拟题目中给出的建树过程,栈顶元素为正在处理的结点,很方便的把每个结点的左右儿子构造好
18 
19 int t;
20 void print(int first)//后缀输出结点
21 {
22     if (first==-1)
23     return;
24     print(no[first].left);
25     print(no[first].right);
26     if (t==0)
27     {
28              t=1;
29     cout <<first;
30 }
31     else
32     cout<<" "<<first;
33 }
34 int main()
35 {
36     int m,n,now,first;//now代表当前处理的结点,first代表树根
37     string str;
38     while (cin>>n)
39     {
40           t=0;
41           while (!s.empty())//第一个肯定是树根
42           s.pop();//入栈
43           
44           cin>>str>>m;
45           first=m;
46           s.push(m);
47           now=m;
48           for (int i=1;i<2*n;i++)
49           {
50                 
51                 cin>>str;
52                 if (str=="Push")
53                 {
54                      cin>>m;
55                      if (no[now].left==-1)
56                      no[now].left=m;
57                      else
58                      no[now].right=m;
59                      now=m;//下一个要处理的结点为m
60                      s.push(m);
61                 }
62                 else
63                 {
64                     now=s.top();//下一个要处理的结点为s.top
65                     s.pop();
66                 }
67           }
68           print(first);
69           cout <<endl;
70     }
71     return 0;
72 }

03-树2. Tree Traversals Again (25)的更多相关文章

  1. 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历

    03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913 An inorde ...

  2. pat03-树3. Tree Traversals Again (25)

    03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. pat1086. Tree Traversals Again (25)

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. PTA 03-树3 Tree Traversals Again (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again   (25分) An inor ...

  5. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  6. 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)

    7-5 Tree Traversals Again (25 分)   An inorder binary tree traversal can be implemented in a non-recu ...

  7. PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]

    题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...

  8. 1086. Tree Traversals Again (25)-树的遍历

    题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...

  9. 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

    题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...

随机推荐

  1. information_schema.character_sets 学习

    information_schema.character_sets 表用于查看字符集的详细信息 1.character_sets 常用列说明: 1.character_set_name: 字符集名 2 ...

  2. linux中如何使用微软鼠标的第4、5键

    虽说使用linux的 人大都对微软没什么好感,但不能否认微软确实也出了不少好东西呀,比如微软鼠标(IE系列) icon_smile.gif IE 2.0和以上版本都有5个按钮,除了正常的左中右外,两侧 ...

  3. USB class总结

    以下是总结的USB的类,不同的USB类,主机询问的描述符格式和内容都不一样,具体的descriptor的内容可以查看 USB spec(http://www.usb.org/home/),如HID的s ...

  4. Keil C -WARNING L15: MULTIPLE CALL TO SEGMENT

    1.第一种错误信息 ***WARNING L15: MULTIPLE CALL TO SEGMENT SEGMENT: ?PR?_WRITE_GMVLX1_REG?D_GMVLX1 CALLER1: ...

  5. socket基础(二)

    Microsoft.Net Framework为应用程序访问Internet提供了分层的.可扩展的以及受管辖的网络服务,其名字空间System.Net和System.Net.Sockets包含丰富的类 ...

  6. 《深入了解 Linq to SQL》之对象的标识 —— 麦叔叔呕心呖血之作

    序言 很多朋友都向我提过,希望我写一下关于Linq to SQL 或者 VS 插件方面的文章.尽管市面上有很多 Linq to SQL 的书籍,但是都是介绍怎么用,缺乏深度.关于 VS 插件方面的书籍 ...

  7. Unity 手指触摸的方向(单手)

    最近写了一个跑酷游戏,总结下里面的知识点:O(∩_∩)O~ using UnityEngine; using System.Collections; public class Demo : MonoB ...

  8. 格而知之7:我所理解的Runtime(2)

    消息发送(Messaging) 8.以上便是runtime相关的一些数据结构,接下来我们回看一开始的疑问: objc_msgSend()函数在执行的过程中是如何找到对应的类,找到对应的方法实现的呢? ...

  9. 精讲N皇后问题

             思想:存三个数组记录记录走的过程,运用回溯不符合或row==n+1就跳出当前层,直到找完:递归时的路径都在保存着,当连续跳出到第一次进入的dfs且i=n时就全部跳出dfs函数了: # ...

  10. LoadRunner性能测试中Controller场景创建需注意的几点

    在LR工具做性能测试中,最关键的一步是Controller场景的设计,因为场景的设计与测试用例的设计相关联,而测试用例的执行,直接影响最终的测试结果是怎么的,因此,我们每设计一种场景,就有可能是一个测 ...