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

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @version: 3.5 @author: morgana @lic ...

  2. myeclipse与eclipse的web项目部署区别

    一.myeclipse之web项目的部署(发布)流程 web项目的部署(发布)流程2008-01-18 14:35 在myeclipse下新建web工程abc.系统设置默认如下: 项目保存位置:wor ...

  3. Ubuntu,kubuntu与xubuntu的差别 Ubuntu各版本主要差异

    Ubuntu各版本主要差异 Ubuntu官方考虑到使用者的不同需求,提供各种不同的发行版.虽然发布了几种版本的Ubuntu系统,但是它们的核心系统是一模一样的.可以这么说不同发行版的Ubuntu的区别 ...

  4. Blender 基础 骨架 01

    Blender 基础 骨架 01 我使用的Blender版本:Blender V 2.77 前言 本讲介绍: 骨架的基本使用方式. 骨架是角色动画里面最常使用的元素,它可以准确控制一个模型的变形,尤其 ...

  5. 最短路径Dijkstar算法和Floyd算法详解(c语言版)

    博客转载自:https://blog.csdn.net/crescent__moon/article/details/16986765 先说说Dijkstra吧,这种算法只能求单源最短路径,那么什么是 ...

  6. 数字图像处理实验(4):PROJECT 02-04 [Multiple Uses],Zooming and Shrinking Images by Bilinear Interpolation 标签: 图像处理MATLAB

    实验要求: Zooming and Shrinking Images by Bilinear Interpolation Objective To manipulate another techniq ...

  7. NSSelectorFromString 使用示例

    NSSelectorFromString 动态加载实例方法. SEL sel = NSSelectorFromString(@"yourMethod:")//有参数 if([obj ...

  8. 数据结构 merge_link合并链表

    问题描述 本题任务是维护一条非递减的链表,初始长度为 0,记这条链表为主链表.对主链表做 N 次操作,操作分两种:1 k a1 a2 … ak,表示一条长度为 k 且非递减的链表,需要将这条链表合并到 ...

  9. DBUtils工具类和DBCP连接池

    今日内容介绍 1.DBUtils2.处理结果集的八种方式3.连接池4.连接池的用法1 PrepareStatement接口预编译SQL语句 1.1 预处理对象 * 使用PreparedStatemen ...

  10. HTML5与CSS3设计模式 中文版 高清PDF扫描版

    HTML5与CSS3设计模式是一部全面讲述用HTML5和CSS3设计网页的教程.书中含350个即时可用的模式 (HTML5和CSS3代码片段),直接复制粘贴即可使用,更可以组合起来构建出无穷的解决方案 ...