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.
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
题意:
采用非递归的方式来确定一颗二叉树,然后将这颗二叉树按照后序遍历的方式来输出。
思路:
这道题用到的理论知识就是通过一棵二叉树的先序遍历和中序遍历来构建这棵树,当然前提是你能够看出来进栈的序列是先序遍历的结果,出栈的序列是中序遍历的结果。构建树的代码(递归):
Node CreateTree(int left, int right){
if(left > right) return NULL;
int root = preOrder[cur];
cur++;
int rootIndex = findRootIndex(root);
Node T = new TreeNode();
T->num = root;
if(left != right){
T->left = CreateTree(left,rootIndex-1);
T->right = CreateTree(rootIndex+1,right);
}
return T;
}
Code:
#include<iostream>
#include<queue>
#include<stack> using namespace std; typedef struct Node *node;
struct Node {
int value;
node leftSon;
node rightSon;
node father;
Node():value(), leftSon(), rightSon(), father(){}
}; void postOrder(node h) {
if (h != NULL) {
postOrder(h->leftSon);
postOrder(h->rightSon);
cout << h->value << " ";
}
} int main() {
int n;
cin >> n;
getchar();
string str, op, num;
int pos;
queue<string> q;
stack<int> s;
stack<string> leftOrRight;
for (int i = 0; i < n*2; ++i) {
getline(cin, str);
if (str[1] == 'u') {
pos = str.find(' ');
op = str.substr(0, pos);
num = str.substr(pos+1);
q.push(op);
q.push(num);
} else {
q.push(str);
}
} node head = new Node();
node prt = head;
while (!q.empty()) {
if (q.front() == "Push") {
q.pop();
int value = stoi(q.front());
s.push(value);
q.pop();
node temp = new Node();
if (prt->leftSon) {
leftOrRight.push("right");
prt->rightSon = temp;
temp->father = prt;
} else {
leftOrRight.push("left");
prt->leftSon = temp;
temp->father = prt;
}
prt = temp;
} else {
q.pop();
if (leftOrRight.top() == "left") prt = prt->father;
prt->value = s.top();
if (leftOrRight.top() == "right") prt = prt->father;
s.pop();
leftOrRight.pop();
}
}
postOrder(prt);
return 0;
}
以上是我写的代码,因为没有注意到题目中暗含着两种遍历,所以写的代码也很乱,当然也是错误的。
以下的代码来自:https://blog.csdn.net/xyt8023y/article/details/47443489
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <sstream>
#include <stack> using namespace std; int N,cur;
vector<int> preOrder;
vector<int> inOrder; typedef struct TreeNode *Node;
struct TreeNode{
int num;
Node left,right; TreeNode(){
left = NULL;
right = NULL;
} }; int findRootIndex(int rootNum){ for(int i = 0;i < N; i++){
if(inOrder[i] == rootNum){
return i;
}
}
return -1; } Node CreateTree(int left, int right){
if(left > right) return NULL;
int root = preOrder[cur];
cur++;
int rootIndex = findRootIndex(root);
Node T = new TreeNode();
T->num = root;
if(left != right){
T->left = CreateTree(left,rootIndex-1);
T->right = CreateTree(rootIndex+1,right);
}
return T;
} bool firstOutPut = true;
void PostOrder(Node T){
if(!T) return;
PostOrder(T->left);
PostOrder(T->right);
if(firstOutPut){
printf("%d",T->num);
firstOutPut = false;
}else{
printf(" %d",T->num);
}
} int main()
{
stringstream ss;
string Nstr;
getline(cin,Nstr);
ss << Nstr;
ss >> N;
ss.clear();
string input;
stack<int> stk;
int value;
for(int i = 0; i < N * 2; i++){
getline(cin,input);
if(input[1] == 'u'){
string num = input.substr(5);
ss << num;
ss >> value;
ss.clear();
stk.push(value);
preOrder.push_back(value);
}else{
value = stk.top();
stk.pop();
inOrder.push_back(value);
}
}
Node T = CreateTree(0,N-1);
PostOrder(T);
return 0;
}
值得注意的是cur并没有赋初值,测试发现他的初值默认为0.
1086 Tree Traversals Again的更多相关文章
- PAT 1086 Tree Traversals Again
PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 1086 Tree Traversals Again[中序转后序][难]
1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- 1086 Tree Traversals Again——PAT甲级真题
1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...
- 1086. Tree Traversals Again (25)
题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]
题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...
- 1086. Tree Traversals Again (25)-树的遍历
题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...
- 1086 Tree Traversals Again (25 分)(二叉树的遍历)
用栈来模拟一棵二叉树的先序遍历和中序遍历过程,求这棵二叉树的后序遍历 由题棵知道:push是先序遍历 pop是中序遍历 #include<bits/stdc++.h> using name ...
随机推荐
- 微信的两种access_token总结,不能混淆
大家需要弄清楚微信的网页授权token和公众号api调用授权token. 1.网页授权access_token 1.有效期:7200ms 2.微信网页授权是通过OAuth2.0机制实现的,在用户授权给 ...
- Win32Api -- 关闭当前应用
本文介绍Windows系统下使用Win32API获取当前应用并关闭的方法. 思路 使用EnumWindows接口枚举当前窗口; 过滤掉不可用.隐藏.最小化的窗口: 过滤掉子窗口: 通过标题.类名过滤掉 ...
- iOS中web与Js的交互
问题 感觉到uni-app框架有pit,公司强推该框架的小哥识趣的闭嘴,考虑到全盘替换周期跟成本挺大,基于uni-app能打包成H5,采用webview+js的原生方式集成 基本结构:原生壳 + we ...
- docker方式部署禅道
一.概述 使用docker方式部署禅道简单,快速,不容易出错.比起编译安装要方便很多. 二.部署 环境说明 操作系统:centos 7.6 ip地址:10.212.82.65 docker版本:19. ...
- 弹性盒布局详解(display: flex;)
弹性盒布局详解 弹性盒介绍 弹性盒的CSS属性 开启弹性盒 弹性容器的CSS属性 flex-direction设置弹性元素在弹性容器中的排列方向 主轴与侧轴(副轴) flex-wrap设置弹性容器空间 ...
- SSRF攻击原理
目录 什么是SSRF 原理 防护 什么是SSRF 一个对外的Web接口,改接口能让用户控制curl命令,去访问别的web服务. 简图如下 想象一下当用户请求的baidu.com/x.php?image ...
- Android Studio|IntelliJ IDEA Git使用小技巧
一 分支管理 1. 新建分支 在master的基础上创建新分支dev 2. 推送分支 将新建的分支dev推送到远程 3. 切换分支 4. 合并分支 当我们在dev分支完成代码修改并测试通过后 需要将d ...
- 手把手教你Spring Boot2.x整合Elasticsearch(ES)
文末会附上完整的代码包供大家下载参考,码字不易,如果对你有帮助请给个点赞和关注,谢谢! 如果只是想看java对于Elasticsearch的操作可以直接看第四大点 一.docker部署Elastics ...
- WPF 基础 - Binding 的源与路径
1. 源与路径 把控件作为 binding 源与 binding 标记拓展: 控制 Binding 的方向及数据更新: Binding 的路径 Path: 没有路径的 Binding: 为 Bindi ...
- java常见面试题3:线程间通信
写两个线程,一个线程打印 1~52,另一个线程打印字母A-Z. 打印顺序为12A34B56C78D--5152Z.要求用线程间的通信. 代码清单: class Printer { private in ...