PAT甲级1119,我先在CSDN上面发布的这篇文章:https://blog.csdn.net/weixin_44385565/article/details/89737224

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, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

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 preorder 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, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. 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 1:
7
1 2 3 4 6 7 5
2 6 7 4 5 3 1
Sample Output 1:
Yes
2 1 6 4 7 3 5
Sample Input 2:
4
1 2 3 4
2 4 3 1
Sample Output 2:
No
2 1 3 4
 题目大意:给定先序和后序序列,输出二叉树的中序遍历序列。若二叉树不唯一则输出任意解。

思路:下一个节点与当前节点在后序数组中紧挨着就说明二叉树不唯一,因为无法判断它是当前节点的左孩子还是右孩子。递归地建立二叉树的时候需要传递四个变量,preLeft、preRight、postLeft、postRight,也就是子树的范围即左右边界在先序和后序数组中的下标。

注意:输出二叉树后要换行!!!不然会格式错误。

 #include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
typedef struct node *BT;
struct node {
int key;
BT left = NULL, right = NULL;
};
int N;
bool flag = false, unique = true;
unordered_map <int, int> postMp;
vector <int> pre, post;
BT buildTree(int preLeft, int preRight, int postLeft, int postRight);
void inorder(BT tree);//中序遍历
int main()
{
int i;
scanf("%d", &N);
pre.resize(N);
post.resize(N);
for (i = ; i < N; i++) {
scanf("%d", &pre[i]);
}
for (i = ; i < N; i++) {
scanf("%d", &post[i]);
postMp[post[i]] = i;
}
BT tree = NULL;
tree = buildTree(, N - , , N - );
printf(unique ? "Yes\n" : "No\n");
inorder(tree);
printf("\n");//结果后面要换行!!!否则就是格式错误,我还能说啥?
}
BT buildTree(int preLeft, int preRight, int postLeft, int postRight) {
BT tree = new node();
tree->key = pre[preLeft];
if (preLeft == preRight) {
return tree;
}
int next = preLeft + ;
if (postMp[pre[preLeft]] - postMp[pre[next]] == ) {
unique = false;
tree->left = buildTree(next, preRight, postLeft, postMp[pre[next]]);
}
else {
int leftNum = postMp[pre[next]] - postLeft;
tree->left = buildTree(next, next + leftNum, postLeft, postMp[pre[next]]);
next = next + leftNum + ;
tree->right = buildTree(next, preRight, postMp[pre[preLeft + ]] + , postMp[pre[next]]);
}
return tree;
}
void inorder(BT tree) {
if (tree) {
inorder(tree->left);
if (flag)
printf(" ");
flag = true;
printf("%d", tree->key);
inorder(tree->right);
}
}

Pre- and Post-order Traversals(先序+后序序列,建立二叉树)的更多相关文章

  1. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  2. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  3. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. [leetcode]从中序与后序/前序遍历序列构造二叉树

    从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 po ...

  5. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  6. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

  7. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  8. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. 【PAT甲级】1119 Pre- and Post-order Traversals (30分)(已知先序后序输出是否二叉树唯一并输出中序遍历)

    题意: 输入一个正整数N(<=30),接着输入两行N个正整数第一行为先序遍历,第二行为后续遍历.输出是否可以构造一棵唯一的二叉树并输出其中一颗二叉树的中序遍历. trick: 输出完毕中序遍历后 ...

随机推荐

  1. H.264学习笔记

    1.帧和场的概念 视频的一场或一帧可用来产生一个编码图像.通常,视频帧可以分成两种类型:连续或隔行视频帧.我们平常看的电视是每秒25帧,即每秒更换25个图像,由于视觉暂留效应,所以人眼不会感到闪烁.每 ...

  2. __call()和__callStatic()方法

    __call() 当对象访问不存在的方法时,__call()方法会被自动调用__callStatic() 当对象访问不存在的静态方法时,__callStatic()方法会被自动调用 这两个方法在PHP ...

  3. static、静态变量、静态方法

    1 静态:static 1.1 用法 是一个修饰符:用于修饰成员(成员变量和成员函数) 1.2 好处 当成员变量被静态static修饰后,就多了一种调用方式,除了可以被对象调用外,还可以直接被类名调用 ...

  4. ROS Learning-024 (提高篇-002) rviz的安装和使用

    ROS 提高篇-002 - rviz 的安装和使用 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LTS ROS 版本:ROS ...

  5. Python-黑客-004 用Python构建一个SSH僵尸网络-02 手动与SSH交互

    用Python构建一个SSH僵尸网络-02 手动与SSH交互 - 登录SSH服务器端的 root 用户 我的电脑(攻击者)的系统:Ubuntu14.04 : 用户名: aobosir@ubuntu:~ ...

  6. docker学习(1)docker的安装

    原文地址:http://blog.csdn.net/we_shell/article/details/38352837 1. 前言 Docker的英文本意是“搬运工”,在程序员的世界里,Docker搬 ...

  7. 福大软工1816 · 第五次作业 - 结对作业2_EXE图片_备用

    1_每日推荐界面.png 2_论文搜索界面.png 2_论文搜索界面_搜索功能.png 3_流行趋势_十大热词排名统计图.png 4_人物界面.png 5_我的收藏界面.png 6_设置界面.png ...

  8. ISE 14.7 XST.exe stop working

    http://www.xilinx.com/support/answers/59851.html Description XST completes but then instead of retur ...

  9. 《Effective Java》第6章 枚举和注解

    第30条:用enum代替int常量 将加班工资计算移到一个私有的嵌套枚举中,将这个策略枚举(strategy enum)的实例传到PayrollDay枚举的构造器中.之后PayrollDay枚举将加班 ...

  10. 简单的使用rabbitmq的例子

    1.创建了两个项目 : (1).spring_cloud_rabbitmq_send 消息发送者 (2).spring_cloud_rabbitmq_receive  消息接受者 2. 添加rabbi ...