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 ...
随机推荐
- 使用idea从svn检出项目
1.创建空的工程 2. 3. 编辑项目进行提交svn服务器进行自动整合
- ios position:fixed 上滑下拉抖动
ios position:fixed 上滑下拉抖动 最近呢遇到一个ios的兼容问题,界面是需要一个头底部的固定的效果,用的position:fixed定位布局,写完测试发现安卓手机正常的,按时ios上 ...
- 2019-8-31-dotnet-数组自动转基类数组提示-Co-variant-array-conversion-是什么问题
title author date CreateTime categories dotnet 数组自动转基类数组提示 Co-variant array conversion 是什么问题 lindexi ...
- VBA中msgbox的用法小结
1.作用在消息框中显示信息,并等待用户单击按钮,可返回单击的按钮值(比如“确定”或者“取消”).通常用作显示变量值的一种方式.2.语法MsgBox(Prompt[,Buttons][,Title][, ...
- C++: string<-->char
1. char*.char[] 与 std::string 之间的区别: char*是一个指向字符的指针,是一个内置类型.可以指向一个字符,也可以表示字符数组的首地址(首字符的地址).我们更多的时候是 ...
- 传统的dom的渲染方式
DOM渲染的过程大致分为三个阶段: 后端渲染 前端渲染 独立DOM渲染(前后端相结合渲染) 1.后端渲染:DOM树的生成完全是在后端服务器中完成的,后端服务器的程序会把需要的数据拼合成一个类似于前端D ...
- window 系统上传文件到linux 系统出现dos 格式换行符
Windows里的文件在Unix/Mac下打开的话,在每行的结尾可能会多出一个^M符号,Unix/Mac系统下的文件在Windows里打开的话,所有文字会变成一行,所以为了避免这种情况的发生,我们可以 ...
- leetcode-47-全排列②
题目描述: 方法一:回溯 class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: #nums = ...
- 17个方法防止dedeCMS织梦网站被黑挂木马
dede织梦cms系统的程序存在漏洞,黑客攻击方法层出不穷,导致网站经常被黑,被百度安全中心等拦截,影响排名和流量,让站长非常头疼,下面总结一些防止dede织梦cms系统被攻击设置的方法,可有效的防止 ...
- 数组那些事(slice,splice,forEach,map,filter等等)
周五,再过会要下班了,刚才把<javascript高级程序设计>数组这块又看了下,加深下记忆.今天来继续练练笔,嘿嘿!(写下自己印象不深的东西) 一.数组的定义(数组定义分为两种) 方法一 ...