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】的更多相关文章

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

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

  2. pat 甲级 1127. ZigZagging on a Tree (30)

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

  3. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  4. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  5. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  6. PAT 甲级 1127 ZigZagging on a Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...

  7. 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 ...

  8. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  9. 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 ...

随机推荐

  1. canvas 压缩图片上传

    问题:前端开发过程中难免会将数据提交到后台,但若是提交的数据过大,特别上传图片这类需求,如果不对上传的图片进行压缩处理,就难免会出现请求时间过长的情况,对于用户体验肯定就不是太友好,那么这时候该如何将 ...

  2. redis安装到本地服务的方法

    要安装Redis,首先要获取安装包. Windows的Redis安装包需要到以下GitHub链接找到. 链接:https://github.com/MSOpenTech/redis 打开网站后,找到R ...

  3. H5全局属性contenteditable,实现可编辑元素

    <div contenteditable="true">这是一段可编辑的段落.请试着编辑该文本.</div> 效果如下:

  4. wordpress翻译插件gtranslate

    https://www.gdstautoparts.com/

  5. 01二维背包+bitset优化——hdu5890

    口胡一种别的解法: 三重退背包,g1[j]k]表示不选x的选了j件物品,体积为k的方案数,g[0][0] = 1 , g1[j][k]=dp[j][k]-g1[j-1][k-a[x]] 然后按这样再退 ...

  6. NX二次开发-UFUN导入表达式UF_MODL_import_exp

    最近在做表达式创建,发现UFUN的创建表达式函数UF_MODL_create_exp,UF_MODL_create_exp_tag没有办法创建字符串类型的表达式,例如AA="BB" ...

  7. Vue-cli中使用vConsole,以及设置JS连续点击控制vConsole按钮显隐功能实现

    最近发现了一个鹅厂的仓库,实现起来比我这个方便[捂脸].https://github.com/AlloyTeam/AlloyLever 一.vue-cli脚手架中搭建的项目引入vConsole调试 1 ...

  8. 6.RabbitMQ Linux安装

    RabbitMQ在Linux上安装,需要很多依赖库,如何不能解决依赖库德版本问题,可能会比较麻烦,最好结合Yum进行安装,我这里使用的Linux环境是64位CentOS6.2 ,使用Yum源是阿里云的 ...

  9. Rootkit之SSDT hook(通过CR0)

    CR0当中有一个写保护位,是保护内存不可写属性的,为了能够写入内核,只能把它的保护给咔嚓掉了,不过--如果做完了手脚但不还原写保护属性的话,极有可能会BOSD. /================== ...

  10. flutter 修饰盒子

    decoration: BoxDecoration( borderRadius: BorderRadius.circular(), //圆角 gradient: RadialGradient( col ...