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. GIT 实验

    服务器环境:linux + git + gitolite(gitolite是什么,说白了就是安装后建了一个仓库,管理员用户可以通过修改并上传配置文件实现GIT仓库及其权限的管理.提醒:别用那个gito ...

  2. ZendFramework 环境部署

    查看源码 int_autoloader.php 文件中,发现应用了一个 AutoloaderFactory 的命名空间,路径写得是相对路径,所以需要在 php.ini 中定义一个 inclde_pat ...

  3. Ext4报错Uncaught Ext.Loader is not enabled

    提示: Uncaught Ext.Loader is not enabled, so dependencies cannot be resolved dynamically. Missing requ ...

  4. 《how to design programs》12章函数复合

    我们写代码时要学会适应辅助函数.作者提出了一个问题,如何对一个表排序.排序函数读取一个表,产生另一个表.排序函数的合约和用途如下: (sort empty) ;; expected value: em ...

  5. Windows搭建Sublime Text 3 + Go开发环境

    1. 安装Sublime Text 3 Sublime Text 3(以下简称ST)的下载与安装我就不说啦,目前还是一个测试版,不过据说比ST2增加了好多新功能,下载地址: http://www.su ...

  6. cf472C Design Tutorial: Make It Nondeterministic

    C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit per test 256 ...

  7. 经典递归算法研究:hanoi塔的理解与实现

    关于hanoi塔的原理以及概念,请Google,访问不了去百度. 主要设计到C中程序设计中递归的实现: 主代码实现如下: void hanoi(int src, int dest, int tmp, ...

  8. Eclipse设置分级折叠显示项目工程路径

    1.抛出问题现象 如下图,这种方式看项目中的代码简直痛苦的要死: 项目迭代越多,工程目录越庞大,可读性就越差. 2.设置分级折叠显示项目 第一步:在Package Explorer视图中找到它的缩放菜 ...

  9. UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>

    E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  10. 探索PHP+Nginx(二) 安装PHP

    首先,我们简单了解一下什么是PHP,PHP(Hypertext Preprocessor 超文本预处理器) 和Java语言一样,PHP也是属于高级语言,并不能直接在操作系统上运行.Java运行需要虚拟 ...