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;
}

最新文章

  1. 当在浏览器输入一个url访问后发生了什么
  2. mybatis int 类型判断&lt;if&gt;
  3. 使用SimpleXML应该注意的问题有哪些?
  4. 支持新版chrome,用webstorm编译形成css和sourcemap,调试sass和less源文件(转)
  5. POJ 1456 Supermarket(贪心+并查集优化)
  6. 【开发工具 - Git】之Git使用案例
  7. java枚举的使用
  8. jQuery / zepto ajax 全局默认设置
  9. Arduino UNO +ESP8266采集数据上传到贝壳网
  10. 利用GPU实现无尽草地的实时渲染
  11. Oracle表之间关联更新
  12. php 两个数组,若键相同,则值合并
  13. IdentityServer4【Topic】Consent
  14. MySQL排序函数field()详解
  15. Py designer 小程序实现实例
  16. web开发中兼容性问题(IE8以上含)持续更新~~
  17. Bootstrap table后端分页(ssm版)
  18. Alpha冲刺——第七天
  19. form 回车键(ENTER)
  20. 微信小程序设计稿pt怎么转rpx

热门文章

  1. JVM调优之垃圾定位、垃圾回收算法、垃圾处理器对比
  2. Inceptor常用SQL
  3. js实现购物车左滑删除
  4. 数据结构-kmp算法
  5. G - 跑跑卡丁车
  6. Codeforces Global Round 11 B. Chess Cheater(贪心)
  7. Codeforces Round #670 (Div. 2) C. Link Cut Centroids (dfs,树)
  8. poj 2566 Bound Found 尺取法
  9. Drone构建失败,一次drone依赖下载超时导致构建失败的爬坑记录
  10. Operating System:信号量