1086 Tree Traversals Again

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.

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

题目大意:用栈来模拟以可二叉树的中序遍历,让你求出这棵二叉树的后序遍历。

大致思路:由题意可知二叉树的输入顺序为二叉树的前序遍历,然后我们借助一个栈来得出二叉树的后续遍历。进行一次Push操作我们就像栈中存一个元素,Pop操作我们就返回栈顶元素并将元素存入数组中。最后得到二叉树的中序遍历序列。

#include <bits/stdc++.h>

using namespace std;

const int N = 65;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int n;
TreeNode root[N];
vector<int> preorder, inorder, postorder; //先序序列,中序序列 void newNode(int x) {
root[x].val = x;
root[x].left = root[x].right = -1;
} //给出了中序和先序构建二叉树
TreeNode* create(int preL, int preR, int inL, int inR) {
if (preL > preR) return -1;
TreeNode* root = new TreeNode(preorder[preL]);
int k;
for (k = inL; k <= inR; k++) {
if (inorder[k] == preorder[preL]) break;
}
int numL = k - inL;
root->left = create(preL + 1, preL + numL, inL, k - 1);
root->right = create(preL + numL + 1, preR, k + 1, inR);
return root; } void postvisit(TreeNode* root) {
if (node == -1) return;
postvisit(root->left);
postvisit(root->right);
postorder.push_back(root->val);
} int main() {
scanf("%d", &n);
//先将每个节点初始化
stack<int> tmp;
string op;
int id;
while(n--) {
cin >> op;
if (op == "Push") {
cin >> id;
tmp.push(id);
preorder.push_back(id);
}
else {
inorder.push_back(tmp.top());
tmp.pop();
}
}
create(0, n - 1, 0, n - 1);
TreeNode* root = create(0, n - 1, 0 , n - 1);
postvisit(root);
for (int i = 0; i < postorder.size(); i++) {
cout << postorder[i];
if (i != postorder.size() - 1) cout << " ";
else cout << endl;
}
return 0;
}

1086 Tree Traversals Again——PAT甲级真题的更多相关文章

  1. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  2. 1020 Tree Traversals——PAT甲级真题

    1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...

  3. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  4. PAT 甲级真题

    1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...

  5. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  6. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  7. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  8. PAT 甲级真题题解(121-155)

    1121 Damn Single 模拟 // 1121 Damn Single #include <map> #include <vector> #include <cs ...

  9. PAT甲级真题 A1025 PAT Ranking

    题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...

随机推荐

  1. telnet | ping

    ping通常是用来检查网络是否通畅或者网络连接速度的命令.  ping www.baidu.com 而telnet是用来探测指定ip是否开放指定端口的. telnet xxx 443 查看443开放没 ...

  2. centos编译安装vim7.4

    ./configure --with-features=huge --enable-fontset --enable-gui=gtk2 --enable-multibyte --enable-pyth ...

  3. C++ 标准模板库(STL):map

    目录 4. map 4.1 map的定义 4.2 map容器内元素的访问 4.3 map常用函数实例解析 4.4 map的常见用途 4. map map翻译为映射,也是常用的STL容器. 在定义数组时 ...

  4. Flink-v1.12官方网站翻译-P028-Custom Serialization for Managed State

    管理状态的自定义序列化 本页面的目标是为需要使用自定义状态序列化的用户提供指导,涵盖了如何提供自定义状态序列化器,以及实现允许状态模式演化的序列化器的指南和最佳实践. 如果你只是简单地使用Flink自 ...

  5. 2019牛客多校 Round3

    Solved:3 Rank:105 治哥出题了 我感动哭了 A Graph Game (分块) 题意:1e5个点 2e5条边 s(x)表示与x点直接相邻的点集合 有两种操作 1种将按输入顺序的边第l条 ...

  6. codeforces 292E. Copying Data

    We often have to copy large volumes of information. Such operation can take up many computer resourc ...

  7. 大数据去重(data deduplication)方案

    数据去重(data deduplication)是大数据领域司空见惯的问题了.除了统计UV等传统用法之外,去重的意义更在于消除不可靠数据源产生的脏数据--即重复上报数据或重复投递数据的影响,使计算产生 ...

  8. 国产网络损伤仪 SandStorm -- 什么是链路规则?

    "链路规则"是网络损伤仪SandStorm(又名弱网测试仪)里面非常重要的功能,主要用于不同仿真链路之间的选择. 如下图的所示:                           ...

  9. woj1002-Genesis woj1003-birthofnoah woj1004-noah's ark

    title: woj1002-Genesis date: 2020-03-05 categories: acm tags: [acm,woj] 输入输出考虑一下.easy #include <i ...

  10. 缓冲区溢出实验 4 内存管理(类似于malloc free)

    实验环境.代码.及准备 https://www.cnblogs.com/lqerio/p/12870834.html vul4 观察foo函数,可见问题在于最后一次tfree(q).由于之前已经tfr ...