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> ...
随机推荐
- Matlab操作矩阵的相关方法
Matlab操作矩阵的相关方法 下面这篇文章主要是对吴恩达老师机器学习中matlab操作的一个整理和归纳 一.基本操作 1.生成矩阵(ones.zeros) A = [1 2;3 4;5 6] ...
- localhost不能访问127.0.0.1可以访问的原因以及解决办法
今天在调试程序的时候,出现了一个奇怪问题,localhost不能访问但127.0.0.1可以访问? localhost与127.0.0.1的概念和工作原理之不同 要比较两个东西有什么不同,首先要弄清两 ...
- ThinkPHP之MVC简析
MVC是一种设计模式.它强制性的使用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型(Model).视图(View).控制器(Controller),它们各自处理自己的任务. 模 ...
- Project Euler 126 - Cuboid layers
这题先是推公式… 狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式 \[f(t,x,y,z)=4(t-1)(x+y+z+t-2)+2(xy+yz+xz)\] 表示长宽高为\(x\).\(y\).\ ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- Linux命令2018-03-01更新
前言:Linux主要应用于服务器端,嵌入式开发和个人pc桌面端 本人wechat:YWNlODAyMzU5MTEzMTQ=. *** GPL GPL是一个开源许可协议,由自由软件基金会创建的.GPL许 ...
- ADO.Net入门(2)
(转载) 作者:可米小子 出处:http://liuhaorain.cnblogs.com 1. 什么是连接池? 在上篇文章<你必须知道的ADO.NET(四) 品味Connection对象> ...
- Scrapy组件之item
Scrapy是一个流行的网络爬虫框架,从现在起将陆续记录Python3.6下Scrapy整个学习过程,方便后续补充和学习.Python网络爬虫之scrapy(一)已经介绍scrapy安装.项目创建和测 ...
- storm-kafka源码走读之KafkaSpout
from: http://blog.csdn.net/wzhg0508/article/details/40903919 (五)storm-kafka源码走读之KafkaSpout 原创 2014年1 ...
- ASP.NET 2.0缓存
MSDN上缓存概述: http://msdn2.microsoft.com/zh-cn/library/726btaeh(VS.80).aspx 一.页输出缓存 1.设置 ASP.NET 页缓存的两种 ...