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. loj2001[SDOI2017]树点染色

    题意:给你一棵树,一开始每个点上的颜色互不相同.三种操作:op1:x到根的路径上的点都染上一种新的颜色.op2:设一条路径的权值为val(x,y),求x到y路径的val.op3:询问x的子树中最大的到 ...

  2. 利用reduce方法,对数组中的json对象去重

    数组中的json对象去重 var arr = [{ "name": "ZYTX", "age": "Y13xG_4wQnOWK1Q ...

  3. Change myself to be better

    发现和改变自己不好的习惯 遇到问题的反应 自己遇到问题的时候,特别是不熟悉的问题的时候就会有点焦虑,这个应该是每个人都会有的,遇到自己不熟悉的或者是没有经历过的东西都会 感觉不舒服,或者是遇到难题或者 ...

  4. thinkphp 模板驱动

    模板引擎驱动完成了第三方模板引擎的支持,通过定义模板引擎驱动,我们可以支持Smarty.TemplateLite.SmartTemplate和EaseTemplate等第三方模板引擎. 默认的模板引擎 ...

  5. NX二次开发-算法篇-创建最大边界包容盒

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...

  6. string反向找位置,分割字符串(只取文件夹路径)

    1 #include <uf.h> 2 #include <uf_part.h> 3 #include <atlstr.h> 4 #include <iost ...

  7. java-day15

    File类 文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作 静态成员 static String pathSeparator 路径分隔符 File.pathSeparator ...

  8. angularJS ng-model与wdatapicker问题记录

    代码: <input type="text" placeholder="开始日期" ng-model="data_start" onF ...

  9. java_函数式编程写法

    package cn.aikang.Test; import org.junit.Test; import java.util.Scanner; import java.util.function.S ...

  10. yolo3使用darknet卷积神经网络训练pascal voc

    darknet本来最开始学的是https://github.com/pjreddie/darknet yolo3作者自己开发的,但是它很久不更新了而且mAP值不好观察,于是另外有个https://gi ...