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 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 <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct Node
{
int v;
Node *l, *r;
Node(int a = -) :v(a), l(nullptr), r(nullptr) {}
};
int n;
vector<int>inOrder, posOrder, zigOrder;
Node* reCreatTree(int inL, int inR, int posL, int posR)
{
if (posL > posR)
return nullptr;
Node* root = new Node(posOrder[posR]);
int k = inL;
while (k<=inR && inOrder[k] != posOrder[posR])++k;//找到根节点
int numL = k - inL;//左子树节点个数
root->l = reCreatTree(inL, k - , posL, posL + numL - );
root->r = reCreatTree(k + , inR, posL + numL, posR - );
return root;
}
void zigOrderTravel(Node* root)
{
if (root == nullptr)
return;
queue<Node*>q;
q.push(root);
zigOrder.push_back(root->v);
bool flag = false;
while (!q.empty())
{
queue<Node*>temp;
vector<int>tt;
while (!q.empty())
{
Node* p = q.front();
q.pop();
if (p->l != nullptr)
{
temp.push(p->l);
tt.push_back(p->l->v);
}
if (p->r != nullptr)
{
temp.push(p->r);
tt.push_back(p->r->v);
}
}
if (flag)
reverse(tt.begin(), tt.end());
zigOrder.insert(zigOrder.end(), tt.begin(), tt.end());
flag = !flag;
q = temp;
}
}
int main()
{
cin >> n;
inOrder.resize(n);
posOrder.resize(n);
for (int i = ; i < n; ++i)
cin >> inOrder[i];
for (int i = ; i < n; ++i)
cin >> posOrder[i];
Node* root = reCreatTree(, n - , , n - );
zigOrderTravel(root);
for (int i = ; i < zigOrder.size(); ++i)
cout << (i > ? " " : "") << zigOrder[i];
return ;
}
PAT甲级——A1127 ZigZagging on a Tree【30】的更多相关文章
- 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
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT 甲级 1127 ZigZagging on a Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...
- 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甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 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 ...
随机推荐
- msql数据库基础
一.数据库操作 1.显示数据库 SHOW DATABASES; SHOW CREATE DATABASE 数据库名称; #数据库的创建信息 2.创建数据库 #utf8 CREATE DATABASE ...
- Spring boot热部署实战
1.介绍 在开发工程中,修改一点儿代码,想看效果就需要重新启动服务,这样会花费大量时间在重启服务上,通过devtools热部署可以大大减少重启服务的时间. 之所以能减少时间,是因为Spring Boo ...
- git使用ssh连接服务器
git如何连接服务器呢? $ ssh -p 22 root@服务器ip 解释:ssh -p 端口号 登录的用户名@IP
- Delphi 字符串函数 StrUtils(大全)
引用单元: StrUtils; 首部 function AnsiResemblesText(const AText, AOther: string): Boolean; $[StrUtils.pas ...
- 如何打造7*24h持续交付通道?阿里高级技术专家的5点思考
我们对于研发效能的讨论,本质上是提高整个技术生态中的协同效率.如果仅从研发角度出发,技术团队要实现的终极目标是7*24小时的灵活发布窗口,以及更快的业务迭代能力. 7*24小时发布窗口的实现其实并不简 ...
- NX二次开发-UFUN特征找xxx UF_MODL_ask_feat_xxx等函数(待补充)
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> #include <u ...
- C++ 字符串的分割函数split 及 用法【转载】
文章出处https://blog.csdn.net/glmushroom/article/details/80690881 之前在C#中总用到字符串的分割,使用系统函数即可,比如: string a ...
- HDU3605: Escape-二进制优化建图-最大流
目录 目录 思路: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 题意:传送门 原题目描述在最下面. \(n(n\leq 100000)\)个人\(m(m\leq 10) ...
- 其它课程中的python---2、NumPy模块
其它课程中的python---2.NumPy模块 一.总结 一句话总结: numpy在数组计算方面又快又方便 1.NumPy中的ndarray是一个多维数组对象,该对象由哪两部分组成? -实际的数据 ...
- 5.RabbitMQ 客户端控制消息
1.生产者发送消息,消费者结束消息并回执 2.通过channel.basicConsume向服务器发送回执,删除服务上的消息 3.//不向服务器发送回执,服务器的消息一直存在 4.//消费者拒绝接受消 ...