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 ...
随机推荐
- WCF ServiceContract,OperationContract
代码如下 [ServiceContract] //服务协定定义 using System.ServiceModel; public interface IInterface1 { [Operation ...
- 转载: Android开源库V - Layout:淘宝、天猫都在用的UI框架,赶紧用起来吧!
阿里的UI库... 分析的很精辟... http://blog.csdn.net/carson_ho/article/details/71077193
- 24 The Go image package go图片包:图片包的基本原理
The Go image package go图片包:图片包的基本原理 21 September 2011 Introduction The image and image/color packag ...
- 前端程序员必知的30个Chrome扩展-[转载]
谷歌Chrome浏览器是网络上可用的最好浏览器之一,并且自2011年11月超越了Firefox浏览器之后,已经成为了互联网上占主导地位的浏览器.今天,HTML5中国与大家分享一些实用的谷歌Chrome ...
- POJ 2195 Going Home(KM算法模板)
题目链接:http://poj.org/problem?id=2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致. man每移动一格需花费$1 ...
- GeoHash核心原理解析 - OPEN 开发经验库
阅读目录 引子 一.感性认识GeoHash 二.GeoHash算法的步骤 三.GeoHash Base32编码长度与精度 三.GeoHash算法 四.使用注意点 引子 机机是个好动又好学的孩子,平日里 ...
- (四)HttpClient 使用代理 IP
第一节: HttpClient 使用代理 IP 在爬取网页的时候,有的目标站点有反爬虫机制,对于频繁访问站点以及规则性访问站点的行为,会采集屏蔽IP措施. 这时候,代理IP就派上用场了. 关于代理IP ...
- R vs Python:构建data.frame、读取csv与统计描述
一.Python 数据框就是典型的关系型数据库的数据存储形式,每一行是一条记录,每一列是一个属性,最终构成表格的形式,这是数据科学家必须熟悉的最典型的数据结构. 1.构建数据框 import pand ...
- NopCommerce Plugins 不能智能提示的解决方法(MVC 5 & RAZOR 3.0)
分享给需要的朋友: http://mhammadchehab.com/wordpress/2013/12/enabling-intellisense-for-razor-in-class-librar ...
- USACO 5.3 Network of Schools
Network of SchoolsIOI '96 Day 1 Problem 3 A number of schools are connected to a computer network. A ...