1127 ZigZagging on a Tree
题意:中序序列+后序序列构建二叉树,之字形输出其层序序列。
思路:在结点的数据域中额外增加一个layer表示结点所在的层次,并定义vector<int> zigzag[maxn]存放最终结果。按照常规顺序进行层序遍历,将第i层的值存入到zigzag[i]中,最后输出时,第偶数层从左向右输出,第奇数层反之。
代码:
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
;
int in[maxn],post[maxn];
vector<int> zigzag[maxn];
;
struct Node{
int val;
int layer;
Node *lchild,*rchild;
Node(),lchild(NULL),rchild(NULL){}
};
Node* buildBiTree(int pL,int pR,int inL,int inR)
{
if(pL>pR) return NULL;
int rootval=post[pR];
Node* root=new Node(rootval);
int pos=inL;
while(in[pos]!=rootval) pos++;
int leftCnt=pos-inL;
root->lchild=buildBiTree(pL,pL+leftCnt-,inL,pos-);
root->rchild=buildBiTree(pL+leftCnt,pR-,pos+,inR);
return root;
}
void levelOrderTraversal(Node* root)
{
queue<Node*> q;
root->layer=;
q.push(root);
while(!q.empty()){
Node* pNode=q.front();
q.pop();
if(pNode->layer > maxLayer) maxLayer=pNode->layer;
zigzag[pNode->layer].push_back(pNode->val);
if(pNode->lchild){
pNode->lchild->layer=pNode->layer+;
q.push(pNode->lchild);
}
if(pNode->rchild){
pNode->rchild->layer=pNode->layer+;
q.push(pNode->rchild);
}
}
}
int main()
{
//freopen("pat.txt","r",stdin);
int n;
scanf("%d",&n);
;i<n;i++) scanf("%d",&in[i]);
;i<n;i++) scanf("%d",&post[i]);
Node* root=buildBiTree(,n-,,n-);
levelOrderTraversal(root);
printf("%d",root->val);
;i<=maxLayer;i++){
==){
;j<zigzag[i].size();j++)
printf(" %d",zigzag[i][j]);
}else{
;j>=;j--)
printf(" %d",zigzag[i][j]);
}
}
;
}
1127 ZigZagging on a Tree的更多相关文章
- 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甲级 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
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 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 ...
- 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 1127 ZigZagging on a Tree
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 (30)-中序、后序建树
根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> ...
随机推荐
- mac用ssh连接linux云服务器中文乱码或无法显示解决
问题1:服务器是ubuntu16.04,用mac自带的ssh连接后无法正常输入中文? 解:这种情况一般是终端和服务器的字符集不匹配,MacOSX下默认的是utf8字符集. 打开编辑 .bashrc 文 ...
- C# 运行时替换方法(需要unsafe编译)
/* https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method ...
- 四十五 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的bool组合查询
bool查询说明 filter:[],字段的过滤,不参与打分must:[],如果有多个查询,都必须满足[并且]should:[],如果有多个查询,满足一个或者多个都匹配[或者]must_not:[], ...
- C#的静态构造函数.cctor
静态构造函数操作的是类(而非其实例的)成员.静态构造函数(.cctor)的一些特点:1. 声明和定义形式上,只能有static一个修饰符,不能有任何修饰符和返回值(也不能有void).2. 不能被显示 ...
- Go语言实现FastDFS分布式存储系统WebAPI网关
前言 工作需要,第一次使用 Go 来实战项目. 需求:采用 golang 实现一个 webapi 的中转网关,将一些资源文件通过 http 协议上传至 FastDFS 分布式文件存储系统. 一.Fas ...
- 前端读取Excel报表文件 js-xlsx
1.http://www.cnblogs.com/imwtr/p/6001480.html (前端读取Excel报表文件) 2.https://github.com/SheetJS/js-xlsx
- vue2项目中better-scroll 插件使用时候页面不滚动
参考这里 1.外面包裹层的高度没有设置或者条目的高度没有超过外面包裹层的高度 2.初始化BetterScroll的时机不对,当前元素还没有正常渲染出来,导致BetterScroll的高度的计算错误
- Go语言开发中MongoDB数据库
伴随着移动端的兴起,Nosql数据库以其分布式设计和高性能等特点得到了广泛的应该用,下面将介绍下Nosql中的mongoDB在Go语言中的应用,在开发前,有必要了解下基础知识 在开发前,导入开发需要用 ...
- C++中继承关系中的同名隐藏和对策
在C++及其面向对象的理论中,有这样的场景:一个类继承自另外一个类,如果这两个类都有一个函数名和参数及其返回值一样的成员函数,那么子类的函数会自动将父类对应的函数隐藏.即同名隐藏.在有时的开发过程中, ...
- Rhel7安装及网卡、yum、vmtools配置和修改主机名
(1)安装Vmware WorkStation 11.0 和 RetHatEnterpriseLinux[RHEL]7.0 步骤就不描述了,网上都可以找到 (2)安装VMware Tools )虚拟机 ...