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. 问题:asp.net 点击button按钮调到页面顶部;结果:asp.net点击一个按钮,使页面跳转到本面页上的指定位置

    asp.net点击一个按钮,使页面跳转到本面页上的指定位置 (2011-04-19 16:46:51) 转载▼ 标签: it   最近在做一个项目. 用到标题所说的功能. 实现方法: 1.在aspx中 ...

  2. 部署和调优 2.3 tomcat中JDK安装

    目前有很多网站使用 jsp 的程序编写,所以解析 jsp 的程序就必须要有相关的软件来完成.Tomcat 就是用来解析 jsp 程序的一个软件.因为 Tomcat 技术先进.性能稳定,而且免费,因而深 ...

  3. 探究QA职能

    测试人员一般是被外界普遍认为是QC,即对产品的质量进行检测,找出质量问题并配合相关人员解决问题,从而管控产品质量,说通俗点就是帮开发找漏洞,给开发擦屁股:如果线上出现bug,就是你没有测试完整,最累的 ...

  4. Linux 下安装redis

    记录一下linux下的安装步骤,还是比较复杂的 1. 下载redis-2.8.19.tar.gz: ftp传到linux01上: 解压: tar –zxvf redis-2.8.19.tar.gz 2 ...

  5. 2018网络预选赛 青岛 H

    题目链接:https://pintia.cn/problem-sets/1036903825309761536/problems/1041156323504345088 题意:小明从某一点出发,向右方 ...

  6. 将字符串str1复制为字符串str2的三种方法

    1.自己编写函数,将两个字符串进行复制 #include<iostream> using namespace std; int main(){ char str1[]="I lo ...

  7. 利用JavaScriptCore实现简单的功能(阶乘)

    #import "RootViewController.h" #import <JavaScriptCore/JavaScriptCore.h> @interface ...

  8. UVA1723 Intervals

    这题$n$倍经验…… 考虑差分约束: 我们设$s_i$表示$[-1, i]$这个区间中数字的种类数,那么一个条件的限制相当于$s_{b_i} - s_{a_i - 1} \leq c_i$,那么连边$ ...

  9. jquery checkbox (选中和取消选中事件on("change"))做笔记

    $("#btn_Company").attr("disabled", "disabled"); $("#agency") ...

  10. Spring第五篇

    在Spring第四篇中 我们主要介绍了set get的注入方式 在Spring第五篇中 我们主要介绍使用注解配置Spring 主要分为两个步骤 1 导包的同时引入新得约束 导包如下 1.1 重写注解代 ...