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配置优化和sql语句优化【转】
做优化,我在这里引用淘宝系统分析师蒋江伟的一句话:只有勇于承担,才能让人有勇气,有承担自己的错误的勇气.有承担错误的勇气,就有去做事得勇气.无论做什么事,只要是对的,就要去做,勇敢去做.出了错误,承担 ...
- Ubuntu下使用virtualenv
Ubuntu 18.04,Python 3.6.5(最新3.7),virtualenv 16.0.0, 即将在Ubuntu上大张旗鼓地干活啦!那么,将之前安装的virtualenv运行起来吧(前面都是 ...
- java多线程-读写锁原理
Java5 在 java.util.concurrent 包中已经包含了读写锁.尽管如此,我们还是应该了解其实现背后的原理. 读/写锁的 Java 实现(Read / Write Lock Java ...
- hihoCoder #1183 : 连通性一·割边与割点(求割边与各点模板)
#1183 : 连通性一·割边与割点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 还记得上次小Hi和小Ho学校被黑客攻击的事情么,那一次攻击最后造成了学校网络数据的丢 ...
- 使用django发送邮件时的连接超时问题解决
一.报错 研究报错半天,没看出代码有什么毛病,就是发送邮件时连接超时,发送邮件的连接用户名密码都没有错误,于是就网上各种查... 终于皇天不负有心人,找到答案了.. 在服务器上输入telnet smt ...
- Introduction to MWB Minor Mode
Introduction to MWB Minor Mode */--> Table of Contents 1. Introduction 2. Usage 1 Introduction MW ...
- 20155225 2016-2017-2《Java程序设计》课程总结
20155225 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:新的开始 预备作业2:C语言学习回顾 预备作业3:Linux基础入门和虚拟机的安装 第一 ...
- javascript 去除最后一个字符自定义的方法
//公共去除最后字符方法 function dtrim(str, s){ var reg = eval("/"+s+"$/gi"); str=str.repla ...
- 【LOJ】#2055. 「TJOI / HEOI2016」排序
题解 看错题了,我以为是询问Q是个数字,问它在哪个位置 我一想这不直接01序列搞一下就好了嘛(事实上是012) 然后呢,我发现样例没过. 啊我看错题了,问的是Q这个位置是啥-- 哦,套用我之前的想法不 ...
- Ionic入门五:表单
一.输入框 list 类同样可以用于 input 元素.item-input 和 item 类指定了文本框及其标签. 1.输入框属性:placeholder 以下实例中,默认为100%宽度(左右两侧没 ...