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. nodejs route的简单使用

    demo var express=require('express'); var app=express(); var routeUser=express.Router(); var routeTea ...

  2. 去sqlserver日志

    USE [master] GO ALTER DATABASE DNName SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE DNName SET  ...

  3. leetcode -有效的字母异位词 python&C++

    C++解题代码: class Solutiion { public: bool isAnagram(string s, string t) { ](); int n = s.length(); int ...

  4. 天猫精灵业务如何使用机器学习PAI进行模型推理优化

    引言 天猫精灵(TmallGenie)是阿里巴巴人工智能实验室(Alibaba A.I.Labs)于2017年7月5日发布的AI智能语音终端设备.天猫精灵目前是全球销量第三.中国销量第一的智能音箱品牌 ...

  5. thinkphp模块设计

    3.2发布版本自带了一个应用目录结构,并且带了一个默认的应用入口文件,方便部署和测试,默认的应用目录是Application(实际部署过程中可以随意设置). 通常情况下3.2无需使用多应用模式,因为大 ...

  6. storm集群的安装

    storm图解 storm的基本概念 Topologies:拓扑,也俗称一个任务 Spoults:拓扑的消息源 Bolts:拓扑的处理逻辑单元 tuple:消息元组,在Spoults和Bolts传递数 ...

  7. Javascript下拉刷新

    Html相关代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  8. NOIp2018集训test-9-6(pm)

    T1T2是洛谷原题.Orz辉神290,被辉神吊起来打. 题 1 包裹快递 二分答案.这题似乎卡精度,不开long double二分500次都过不去. //Achen #include<algor ...

  9. 李宏毅机器学习课程---3、Where does the error come from

    李宏毅机器学习课程---3.Where does the error come from 一.总结 一句话总结:机器学习的模型中error的来源是什么 bias:比如打靶,你的瞄准点离准心的偏移 va ...

  10. centos7.4安装高可用(haproxy+keepalived实现)kubernetes1.6.0集群(开启TLS认证)

    目录 目录 前言 集群详情 环境说明 安装前准备 提醒 一.创建TLS证书和秘钥 安装CFSSL 创建 CA (Certificate Authority) 创建 CA 配置文件 创建 CA 证书签名 ...