pat 1127. ZigZagging on a Tree (30) 递归建树 + BFS

1.zigzagging on a tree(C语言实现) 文章目录7-1 zigzagging on a tree1. Question:Introduction:Input Specification:Output Specification:Sample Input:Sample Output:2. Solution2.1 递归建树2.2 双堆栈之字遍历3. Source code 7-1 zigzagging on a tree 1. Question: Introduction: ​ Suppose that all the keys in a bina 阅读详情

1127. ZigZagging on a Tree (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<= 30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the zigzagging sequence of the tree in a line. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1
Sample Output:
1 11 5 8 17 12 20 15

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>

using namespace std;

int N;
int in[35], post[35];
int len, maxdep;
map<int, int> ls, rs;
vector<int> G[35];

struct node {
	int x, dep;
	node() {}
	node(int x, int dep) : x(x), dep(dep) {}
};

int build(int l, int r) {
	if (l > r) {
		return -1;
	}
	int root, i;
	root = post[len--];
	for (i = l; i <= r; i++) {
		if (in[i] == root) {
			break;
		}
	}
	rs[root] = build(i + 1, r);
	ls[root] = build(l, i - 1);
	return root;
}

void bfs(int s) {
	queue<node> q;
	while (!q.empty()) {
		q.pop();
	}
	q.push(node(s, 1));
	for (int i = 0; i < 33; i++) G[i].clear();
	maxdep = -1;
	while(!q.empty()) {
		node tn = q.front(); q.pop();
		maxdep = max(maxdep, tn.dep);
		G[tn.dep].push_back(tn.x);
		if (ls[tn.x] != -1) {
			q.push(node(ls[tn.x], tn.dep + 1));
		}
		if (rs[tn.x] != -1) {
			q.push(node(rs[tn.x], tn.dep + 1));
		}
	}
}

void output() {
	int cnt = 0;
	for (int i = 1; i <= maxdep; i++) {
		if (i % 2) {
			for (int j = G[i].size() - 1; j >= 0; j--) {
				printf(cnt++ == 0 ? "%d" : " %d", G[i][j]);
			}
		} else {
			for (int j = 0; j < G[i].size(); j++) {
				printf(cnt++ == 0 ? "%d" : " %d", G[i][j]);
			}
		}
	}
	puts("");
}

int main()
{
	cin >> N;
	for (int i = 1; i <= N; i++) {
		scanf("%d", &in[i]);
	}
	for (int i = 1; i <= N; i++) {
		scanf("%d", &post[i]);
	}
	len = N;
	ls.clear();
	rs.clear();
	int root = build(1, N);
	bfs(root);
	output();
	return 0;
}





【PAT甲级】1127 ZigZagging on a Tree Z字型遍历二叉树 Z字型遍历 阅读详情

相关推荐

SAP 关于WPS上传文件提示“文档中不包含数据“ 和“程序正在运行中“处理办法

最近这一周经常有用户反馈,使用WPS上传文件系统报错,一直提示“程序正在运行中”,另外在导入数据的时候提示"文档中不包含数据",跟进运维过程中遇到的这两种情况,分享一下处理的办法,如果各位更好的办法也可以给我留言分享。

我小时候很黑的博客 2843

1127. ZigZagging on a Tree

思路:通过递归,利用后序遍历和中序遍历进行建树,最后返回根结点,输出时,使用了两个辅助栈,单数栈和双数栈,单数栈对应的单数行的节点,单数栈压入栈的时候先压左节点,后压右节点,并且将结点压入双数栈,两个栈互相交替。并在压入栈的同时输出该结点的值。#include&lt;iostream&gt; #include&lt;queue&gt; #include&lt;stack&gt; #define M...

bxg1065283526的博客 524

从原理到调试:手把手教你用Matlab解码互补格雷码与相移码(附数据与常见错误排查)

本文详细介绍了如何使用Matlab解码互补格雷码与相移码,涵盖从原理到调试的全过程。通过实际案例和代码示例,帮助读者掌握结构光三维重建中的相位解包裹技术,并提供了常见错误排查与优化建议,提升解码效率与准确性。

weixin_29090917的博客 218

【PAT刷题甲级】1127.ZigZagging on a Tree

1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print th

C_greenbird的博客 584

DFX从芯片到系统:可制造性、可测试性与可靠性设计实战指南

DFX(Design for eXcellence)是一套面向产品全生命周期的设计方法论,涵盖可制造性(DFM)、可测试性(DFT)、可靠性(DFR)等维度。其核心原理是将制造、测试、维护等环节的约束前置到设计阶段,从而降低流片与量产风险。在芯片与系统硬件领域,DFX不仅是提升良率与测试效率的关键手段,更是保障车规、工控等严苛场景下长期稳定运行的基础。从芯片物理实现到PCB布局,再到系统级散热与维护设计,DFX的落地需要工具链、流程闸门与跨部门协作的体系化支撑。本文基于实际项目经验,分享DFX从设计决策到工

weixin_32349093的博客 348

从逻辑门到存储阵列:NAND与NOR闪存命名与结构原理详解

在数字电路与半导体存储器领域,逻辑门是构建一切复杂功能的基础单元,其输入输出关系定义了计算与存储的基本原理。与非门(NAND)和或非门(NOR)作为通用逻辑门,其真值表和行为模式深刻影响了芯片设计。基于这些原理,现代闪存技术发展出了两种核心架构:NAND Flash和NOR Flash。这两种闪存的命名直接源于其内部存储单元阵列的连接方式,分别模拟了与非门和或非门的逻辑关系。这种结构差异决定了它们根本的技术价值:NOR Flash采用并联结构,支持快速随机读取,适用于代码存储与原地执行(XiP);而NAND

weixin_33377602的博客 337

1127 ZigZagging on a Tree

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple stand...

weixin_44270812的博客 194

从零到一:海外短剧成品系统源码搭建流程详解及APP+H5交付服务

在当今全球化的数字娱乐浪潮中,海外短剧市场以其独特的文化魅力和广阔的受众基础,正逐渐成为内容创作者与平台运营者争相布局的新蓝海。我为了紧跟时代的发展我翻看了大量的文章,偶然间看到了一篇文章点进去发现他们是专业做短剧开发的,总结下来在这篇文章里分享给大家。

liangyill_的博客 907

【PAT】1127. ZigZagging on a Tree (30)【树的遍历】

题目描述 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple ...

tjj1998的博客 425

深入浅出一文搞懂Raft协议

前言 在分布式的世界里,要说最核心最复杂的功能,一致性的实现无出其右,之前的paxos算法堪称经典,被认为是同类算法中效果最好的,基本上成为分布式一致性的代名词,但是paxos算法也是出了名的难理解,而且相当不好实现。本人也花了很多时间、看了很多材料也没有真正理解。所以基于paxos的思想进行的一致性算法的简化和实现就成为了现实的需求,在此背景下,本文的主角Raft就出现了。 Raft算法的头号目标就是容易理解(UnderStandable),这从论文中就可以看出来。当然,R...

microGP的专栏 6485

零基础入门红外与可见光图像融合:DAF-Net 实战全指南

本文介绍了基于DAF-Net的红外与可见光图像融合技术,主要内容包括:技术原理、环境配置、数据准备和模型实现。DAF-Net通过双分支特征提取和动态注意力融合机制,有效结合红外图像的热辐射信息与可见光图像的纹理细节。文章详细讲解了开发环境搭建步骤(Python 3.8+PyTorch),TNO数据集的预处理方法,并提供了完整的DAF-Net实现代码,包含特征提取、注意力融合和图像重建三个关键模块。该技术可广泛应用于安防监控、自动驾驶等领域,帮助开发者快速入门多模态图像融合。

FJN110的博客 122

PAT 1127 ZigZagging on a Tree

PAT 1127 ZigZagging on a Tree 树的建立+层序遍历

2301_80219163的博客 217

LLC谐振变换器软启动,不只是“慢启动”,还是“智能启动”

LLC谐振变换器的软启动,不只是“慢慢来”,而是要在控制时序、限流、响应速度之间精妙权衡。而数字控制,特别是用查表法或混合信号法,为实现真正的“最佳轨迹”提供了可能。✨小Tips:如果你的系统成本敏感,选择查表法准没错;如果你的设计目标是超高速响应,那就上混合信号的高配方案!

weixin_43199439的博客 691

【PAT甲级 - C++题解】1127 ZigZagging on a Tree

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 655

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

PAT——1127 ZigZagging on a Tree 甲级

1127 ZigZagging on a Tree题目题意代码解析AC代码参考 题目 https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 题意 给出一棵树的中序和后序序列,要求按Z字型输出该数的层序序列 代码解析 详见参考文章,里面博主说的很详细了,在此解释一下df函数中的第一个参数index: 因为是地址传递,所以这个参数相当于先把postr赋值给root,然后在下一层递归时把root的左孩子和右孩子赋

ljhsq的博客 170

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

pat 甲1127. ZigZagging on a Tree (已知后序及中序建树,并层次往返输出)

1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppose that all the keys in a binary

ninesun的博客 3127

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

pat 甲级 A1127 ZigZagging on a Tree (30分)

1127 ZigZagging on a Tree (30分) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print th

weixin_42358356的博客 150

1127. ZigZagging on a Tree (30)[二叉树构建-dfs-bfs]

1. 原题:https://www.patest.cn/contests/pat-a-practise/1127 2. 思路: 题意:二叉树生成问题。 给出中序和后序,求出树的一定规则的层序。 思路: 中序和后序可以生成唯一的一颗二叉树。 所以先递归构造一颗二叉树。 题目不一样的地方在于层序输出时要求奇数行逆序输出。 一个思路是,我们先把每一层的结果保存到数组中, 输出的时候,奇

坚不萌 355

PAT甲级1127 ZigZagging on a Tree (30分)|C++实现

一、题目描述 原题链接 Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the

weixin_42393947的博客 302

pat 1127. ZigZagging on a Tree

原题链接:https://www.patest.cn/contests/pat-a-practise/1127 1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standar

messenger的博客 423

PAT (Advanced Level) Practice A1127 ZigZagging on a Tree (30 分) (中后序建树、层次遍历、栈)

原题链接 #include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cmath&gt; #include &lt;ctime&gt; #include &lt;cstring&gt; #include &lt;strin

izhxxx的博客 205

PAT(甲级)2017年春季考试 PAT 1127 ZigZagging on a Tree (30 分) 二叉树

PAT(甲级)2017年春季考试 PAT 1127 ZigZagging on a Tree (30 分) 二叉树 【题目描述】 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a sim

weixin_46283740的博客 321

PAT (Advanced Level) Practice 1127 ZigZagging on a Tree 中后序建树、层序遍历变形

一、概述 给出一棵二叉树的中序和后序遍历,求出它的z字遍历。 观察z字遍历我们可以知道,它的奇数层与层序遍历相同,偶数层与层序遍历不同。因此可以选择在层序遍历的基础上修改。 层序遍历来自BFS,使用队列。而使用队列明显无法满足输出要求。将样例写一遍可以发现其具有栈的性质。因此选择使用栈。 但是一个栈不够用,尝试使用两个栈。发现仍有错误。 使用两个栈,分别选择左右入栈和右左入栈。 得到正...

四轩茶屋 223

PAT1127 ZigZagging on a Tree

题目链接: PAT1127 ZigZagging on a Tree 思路: 根据中序和后序遍历创建一棵树; 巧妙的地方在于在创建的时候同时统计每一层的孩子数目 然后层序遍历,根据奇偶性正向或者反向输出即可 #include &lt;iostream&gt; #include&lt;cstring&gt; #include&lt;queue&gt; #include&lt;vector&gt; u...

机器学习,数据挖掘算法爱好者 429

pat根据中序遍历和先序遍历_PAT甲级1127-ZigZagging on a Tree(后序中序建树,层序遍历)...

一.题目Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple s...

weixin_39740346的博客 149

1127. ZigZagging on a Tree (30) PAT 甲级

传送门#include<stdio.h> #include<queue> #include<algorithm> #define MAX_N 32using namespace std; typedef struct Node { Node *lchild; Node *rchild; int data; int level; }node,*BTtree;int i

SY_Yu的博客 1083
上一篇: pat 1118. Birds in Forest (25) 并查集
下一篇: 蓝桥杯 历届试题 PREV-12 危险系数 并查集找割点 Java
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值