1086 Tree Traversals Again——PAT甲级真题
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;
}
最新文章
- 当在浏览器输入一个url访问后发生了什么
- mybatis int 类型判断<;if>;
- 使用SimpleXML应该注意的问题有哪些?
- 支持新版chrome,用webstorm编译形成css和sourcemap,调试sass和less源文件(转)
- POJ 1456 Supermarket(贪心+并查集优化)
- 【开发工具 - Git】之Git使用案例
- java枚举的使用
- jQuery / zepto ajax 全局默认设置
- Arduino UNO +ESP8266采集数据上传到贝壳网
- 利用GPU实现无尽草地的实时渲染
- Oracle表之间关联更新
- php 两个数组,若键相同,则值合并
- IdentityServer4【Topic】Consent
- MySQL排序函数field()详解
- Py designer 小程序实现实例
- web开发中兼容性问题(IE8以上含)持续更新~~
- Bootstrap table后端分页(ssm版)
- Alpha冲刺——第七天
- form 回车键(ENTER)
- 微信小程序设计稿pt怎么转rpx
热门文章
- JVM调优之垃圾定位、垃圾回收算法、垃圾处理器对比
- Inceptor常用SQL
- js实现购物车左滑删除
- 数据结构-kmp算法
- G - 跑跑卡丁车
- Codeforces Global Round 11 B. Chess Cheater(贪心)
- Codeforces Round #670 (Div. 2) C. Link Cut Centroids (dfs,树)
- poj 2566 Bound Found 尺取法
- Drone构建失败,一次drone依赖下载超时导致构建失败的爬坑记录
- Operating System:信号量