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. Dart编程语法

    语法定义了一组用于编写程序的规则.每种语言规范都定义了自己的语法.Dart语法有如下内容组成: 变量和运算符 类 函数 表达式和代码块 判断和循环结构 注释 库和包 类型定义 数据结构表示:集合/泛型 ...

  2. 排列+函数映射——hdu6038好题

    /* 引理:[0,n-1]的排列,i向a[i]连边,那么每个数必定在一个环中 所以数组a可以分割成一些环,数组b也可以分割成一些环 先讨论a的一个环 a[a1]=a2 a[a2]=a3 a[a3]=a ...

  3. DELPHI中如何闪烁应用程序窗口或任务栏按钮

    使用FlashWindowEx函数: 一.设置FlashWInfoDelphi中TFlashWInfo申明如下:TypeTFlashWInfo = record cbSize : LongInt; h ...

  4. NX二次开发-UFUN创建倒圆UF_MODL_create_blend

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建块 UF_FEATURE_SIGN S ...

  5. KiFastCallEntry() 机制分析

    1. 概述 从 windows xp 和 windows 2003 开始使用了快速切入内核的方式提供系统服务例程的调用. KiFastCallEntry() 的实现是直接使用汇编语言,C 语言不能直接 ...

  6. sqoop2安装总结

    sqoop2安装 1. 下载解压缩 此次安装版本为1.99.6 # Decompress Sqoop distribution tarball tar -xvf sqoop-<version&g ...

  7. iOS开发静态库冲突——如何查看静态库(.O)中方法名

    1.bug产生 应用第三方静态库之后提示冲突错误: 2.bug分析 一般会提示哪两个库冲突: CameraShowGLView.o是自己创建的类编译生成的: libLechangeSDK.a是添加的静 ...

  8. 3.7.4 Tri0 and tri1 nets

    Frm: IEEE Std 1364™-2001, IEEE Standard Verilog® Hardware Description Language The tri0 and tri1 net ...

  9. pandas 使用dataframe 索引项相同时出现bug

    使用的是join函数来合并两个dataframe: df=df2.join(df1) bug:columns overlap but no suffix specified: Index([u'muk ...

  10. solr添加IK分词和自己定义词库

    下载IK分词IK Analyzer 2012FF_hf1.zip 下载地址:http://yunpan.cn/cdvATy8899Lrw (提取码:c10d) 1.将IKAnalyzer2012FF_ ...