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 ...
随机推荐
- 深入理解MySQL的并发控制、锁和事务【转】
本文主要是针对MySQL/InnoDB的并发控制和加锁技术做一个比较深入的剖析,并且对其中涉及到的重要的概念,如多版本并发控制(MVCC),脏读(dirty read),幻读(phantom read ...
- asp.net操作word 配置在IIS上出现的问题
异常: 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问. (异常来自 ...
- restful API 规范(转)
1. URI URI 表示资源,资源一般对应服务器端领域模型中的实体类. URI规范 不用大写: 用中杠-不用下杠_: 参数列表要encode: URI中的名词表示资源集合,使用复数形式. 资源集合 ...
- MySQL学习笔记:从一个表update到另外一个表
# ---- 测试数据 ---- # 表1 CREATE TABLE temp_x AS AS c_id, 1.11 AS c_amount FROM DUAL UNION ALL AS c_id, ...
- 20165203 实验一 Java开发环境的熟悉
实验内容及步骤 实验一 Java开发环境的熟悉-1 建立有自己学号的实验目录. 通过vim Hello.java编辑代码. 编译.运行Hello.java代码. 实验一 Java开发环境的熟悉-2 新 ...
- Linux密码策略-密码长度-密码复杂度
1.设置密码长度 vim /etc/pam.d/system-authpassword requisite pam_cracklib.so try_first_pass retry=3 minlen= ...
- mysql远程访问 登录ERROR 1130: is not allowed to connect to this MySQL server解决办法
LINUX6.3 里装了mysql5.0.18 版本运行服务器. 提示错误为: ERROR 1130: Host '192.168.0.102' is not allowed to connect t ...
- USACO 5.3 Window Area
Window AreaIV Balkan Olympiad You've just be assigned the project of implemented a windowing interfa ...
- 微信公众号开发--用.Net Core实现微信消息加解密
1.进入微信公众号后台设置微信服务器配置参数(注意:Token和EncodingAESKey必须和微信服务器验证参数保持一致,不然验证不会通过). 2.设置为安全模式 3.代码实现(主要分为验证接口和 ...
- CentOS7的一些指令
hostnamectl --static set-hostname yuanxu#永久修改主机名 systemctl stop firewalld.service #停止firewall system ...