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 ...
随机推荐
- Task多线程进行多进程
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
- 【转载】ajaxFileUpload 报这错jQuery.handleError is not a function
今天刚打个一个技术群,里面有人问标题上的问题,嘿,我恰好遇过,现在大家至少也在用jquery1.9以上的版本,ajaxfileupload的版本早就不更新了,大家可以下载看:地址这里,它例子里使用的J ...
- Ubuntu下软件安装方式、PATH配置、查找安装位置
Ubuntu 18.04, 安装方式 目前孤知道的Ubuntu下安装软件方式有3种(命令): 1.make 2.apt/apt-get 3.dpkg 方式1基于软件源码安装,需要经历配置(可选).编译 ...
- spring boot 中使用redis session
spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...
- WebApi参数问题方案
原文:http://www.cnblogs.com/landeanfen/p/5337072.html
- TF-tf.nn.dropout介绍
官方的接口是这样的 tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None) 根据给出的keep_prob参数,将输入te ...
- P2471 [SCOI2007]降雨量
Description 我们常常会说这样的话:"X年是自Y年以来降雨量最多的".它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2 ...
- redis持久化的两种方式
redis是一个内存型数据库.当redis服务器重启时,数据会丢失.我们可以将redis内存中的数据持久化保存到硬盘的文件中. redis持久化有两种机制.RDB与AOF.默认方式是RDB. 1.RD ...
- Java 泛型和类型安全的容器
使用java SE5之前的容器的一个主要问题就是编译器允许你向容器插入不正确的类型,例如: //: holding/ApplesAndOrangesWithoutGenerics.java // Si ...
- HDU 4283 You Are the One(区间DP(最优出栈顺序))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4283 题目大意:有一群屌丝,每个屌丝有个屌丝值,如果他第K个上场,屌丝值就为a[i]*(k-1),通过 ...