PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree
题意:
假设二叉树中的所有键都是不同的正整数。一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定。这是一个简单的标准程序,可以按顺序打印数字。但是,如果您认为问题太简单,那么您太天真了。
这次你应该以“锯齿形顺序”打印数字 - 也就是说,从根开始,逐级打印数字,从左到右交替,从右到左。例如,对于以下树,您必须输出:1 11 5 8 17 12 20 15。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数N(<= 30),即二叉树中的总节点数。第二行给出了无序序列,第三行给出了后序序列。一行中的所有数字都以空格分隔。
输出规格:
对于每个测试用例,打印一行树的之字形序列。一行中的所有数字必须只有一个空格分开,并且行尾不能有额外的空格。
思路:
二叉树建立和遍历。用两个栈模拟zigzag遍历。
ac代码:
C++
// pat1127.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<stack>
#include<unordered_map>
#include<unordered_set>
using namespace std;
int n;
int in[31];
int post[31];
int res[31];
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x) , left(NULL) , right(NULL) {}
};
TreeNode* build(int ileft, int iright, int pleft, int pright)
{
if (ileft > iright || pleft > pright) return NULL;
TreeNode* root = new TreeNode(post[pright]);
int cut = ileft;
while (cut <= iright && in[cut] != post[pright]) cut++;
root->left = build(ileft, cut - 1, pleft, pleft + cut - ileft - 1);
root->right = build(cut + 1, iright, pleft + cut - ileft, pright - 1);
return root;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &in[i]);
for (int i = 0; i < n; i++) scanf("%d", &post[i]);
TreeNode* root = build(0, n - 1, 0, n - 1);
//zigzag traversal
stack<TreeNode*> odd;
stack<TreeNode*> even;
odd.push(root);
int pos = 0;
while (!odd.empty() || !even.empty())
{
TreeNode* top;
if(even.empty())
{
while (!odd.empty())
{
top = odd.top();
odd.pop();
res[pos++] = top->val;
if (top->right) even.push(top->right);
if (top->left) even.push(top->left);
}
}
else
{
while (!even.empty())
{
top = even.top();
even.pop();
res[pos++] = top->val;
if (top->left) odd.push(top->left);
if (top->right) odd.push(top->right);
}
}
}
for (int i = 0; i < n - 1; i++)
printf("%d ", res[i]);
if (n > 0) printf("%d\n", res[n - 1]);
return 0;
}
PAT甲级1127. ZigZagging on a Tree的更多相关文章
- PAT甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- pat 甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 甲级 1127 ZigZagging on a Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...
- PAT甲级——A1127 ZigZagging on a Tree【30】
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
- PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]
题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...
- PAT 1127 ZigZagging on a Tree[难]
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- 1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- PAT 1127 ZigZagging on a Tree
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
随机推荐
- python几种装饰器的用法
用函数装饰函数 这种比较常见首先定义装饰器函数 def cache(func): data = {} @wraps(func) def wrapper(*args, **kwargs): key = ...
- RobotFramework安装扩展库包autoitlibrary(四)
Robot Framework扩展库包 http://robotframework.org/#libraries 一,自动化测试PC端程序 1, 安装pywin32(autoitlibrary使用需 ...
- [HBase]mem store flusher 流程
- 数据结构之线性表(python版)
数据结构之线性表(python版) 单链表 1.1 定义表节点 # 定义表节点 class LNode(): def __init__(self,elem,next = None): self.el ...
- CentOS/Linux 网卡设置 IP地址配置
CentOS/Linux下设置IP地址 1:临时修改:1.1:修改IP地址# ifconfig eth0 192.168.100.100 1.2:修改网关地址# route add default g ...
- Centos7.3安装和配置jre1.8
在正式环境里 我们可以不安装jdk ,仅仅安装Java运行环境 jre即可: 第一步:下载jre 我们去oracle官方下载下jre http://www.oracle.com/technetwork ...
- SCTF 2014 PWN400 分析
之前没有分析PWN400,现在再开一篇文章分析一下. 这个日志是我做题的一个笔记,就是说我做一步题就记录一下是实时的.所以说可能会有错误之类的. 首先程序是经典的笔记本程序,基本上一看到这种笔记本就知 ...
- jenkins发邮件问题
1.发送邮件不成功 Sending e-mails to: *******@**.cn Finished: FAILURE 解决办法 jenkins-->configure glo ...
- Web安全漏洞深入分析及其安全编码
摘自:http://blog.nsfocus.net/web-vulnerability-analysis-coding-security/ 超全Web漏洞详解及其对应的安全编码规则,包括:SQL注入 ...
- Hadoop错误1(Text类型与String类型)
在此类的博客中,博主主要记录的是在Hadoop实践过程中遇到的一些错误,先上一个代码 protected void map(Object key,Text value, Context context ...