PAT甲级——A1086 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.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2 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 <iostream>
#include <vector>
#include <stack>
#include <string>
using namespace std;
int N;
vector<int>preOrder, inOrder, posOrder;
struct Node
{
int val;
Node *l, *r;
Node(int a = ) :val(a), l(nullptr), r(nullptr) {};
};
Node* createTree(int preL, int preR, int inL, int inR)
{
if (preL > preR)
return nullptr;
Node* root = new Node(preOrder[preL]);
int i;
for (i = inL; i <= inR; ++i)//找到根节点
if (inOrder[i] == preOrder[preL])
break;
int num = i - inL;
root->l = createTree(preL + , preL + num, inL, i - );
root->r = createTree(preL + num + , preR, i + , inR);
return root;
}
void posOrderTree(Node *root)
{
if (root == nullptr)
return;
posOrderTree(root->l);
posOrderTree(root->r);
posOrder.push_back(root->val);
}
int main()
{ cin >> N;
string str;
stack<int>s;
int a;
for (int i = ; i < *N; ++i)
{
cin >> str;
if (str == "Push")
{
cin >> a;
s.push(a);
preOrder.push_back(a);
}
else
{
inOrder.push_back(s.top());
s.pop();
}
}
Node* root = createTree(, N - , , N - );
posOrderTree(root);
for (int i = ; i < N; ++i)
cout << posOrder[i] << (i == N - ? "" : " ");
return ;
}
PAT甲级——A1086 Tree Traversals Again的更多相关文章
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- PAT 甲级 1020 Tree Traversals
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...
- PAT甲级——A1020 Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
随机推荐
- Winform 窗体闪烁 & 任务栏提示
准备: [DllImport("user32.dll")] static extern bool FlashWindowEx(ref FLASHWINFO pwfi); [DllI ...
- C++ vector操作--往列表中添加或更新内容
有个列表,往里面添加内容,如果对象已存在,只更新其属性,否则添加新一项. #include <iostream> #include <string> #include < ...
- java 对象转Map方法Demo
/** * 用于对Object进行解析并且转换成Map键值对的形式 * */ public class ObjectUtils { private static final String JAVAP ...
- VBA提取HTML文件信息
Sub test() Dim html As Object, D As Object, W As Object, arr() Set html = CreateObject("m ...
- Cookie 干货
从前端开发看Cookie Cookie是浏览器端的存储机制 存在意义: 为了解决“如何记住用户信息”而发明的: 当用户访问网页时,他的名字可以存储在cookie中 下次用户访问该页面时,cookie会 ...
- A decorative fence
A decorative fence 在\(1\sim n\)的全排列\(\{a_i\}\)中,只有大小交错的(即任意一个位置i满足\(a_{i-1}<a_i>a_{i+1}ora_{i- ...
- Python 输入字符串找(String)下标 没有返回-1
str = "abcdefg123456"a = input("请输入一个字母或数字:")num = 0result = -1while num < le ...
- 配置文件一applicationContext.xml
p命名空间注入 需要引入xmlns:p="http://www.springframework.org/schema/p" p命名空间注入的特点是使用属性而不是子元素的形式配置Be ...
- Markdown转义字符表
- 11 Python Libraries You Might Not Know
11 Python Libraries You Might Not Know by Greg | January 20, 2015 There are tons of Python packages ...