Pre- and Post-order Traversals(先序+后序序列,建立二叉树)
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(先序+后序序列,建立二叉树)的更多相关文章
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [leetcode]从中序与后序/前序遍历序列构造二叉树
从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 po ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树: SDUT 1489 Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t& ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 【PAT甲级】1119 Pre- and Post-order Traversals (30分)(已知先序后序输出是否二叉树唯一并输出中序遍历)
题意: 输入一个正整数N(<=30),接着输入两行N个正整数第一行为先序遍历,第二行为后续遍历.输出是否可以构造一棵唯一的二叉树并输出其中一颗二叉树的中序遍历. trick: 输出完毕中序遍历后 ...
随机推荐
- MongoDB数据导入hbase + 代码
需求: 从mongoDB里面查出来数据,判断是否有该列簇,如果有则导入此条数据+列簇,如果没有,则该条数据不包含该列簇 直接贴出代码: package Test; import java.util.A ...
- Bytes和bits的区别(字节和位的区别)
基本概念 Bit意为“位”或“比特”,是计算机运算的基础,属于二进制的范畴: Byte意为“字节”,是计算机文件大小的基本计算单位: 这两者应用的场合不同.通常用bit来作数据传输的单位,因为物理层, ...
- SQL基础E-R图基础
ER图分为实体.属性.关系三个核心部分.实体是长方形体现,而属性则是椭圆形,关系为菱形. ER图的实体(entity)即数据模型中的数据对象,例如人.学生.音乐都可以作为一个数据对象,用长方体来表示, ...
- js对象简单、深度克隆(复制)
javascript的一切实例都是对象,只是对象之间稍有不同,分为原始类型和合成类型.原始类型对象指的是字符串(String).数值(Number).布尔值(Boolean),合成类型对象指的是数组( ...
- 【总结整理】关于ArcGIS中拓扑的理解
空间拓扑: https://www.baidu.com/link?url=f8fd1d75GhwtT1JuyPDZydZlWCgEXB9DeuTzDqGQIIRpq0bM-8t3MlC5tXYvEwQ ...
- 给大家推荐 用 hBuilder编写代码非常好用
截图 可以试哈
- Entity Framework Tutorial Basics(23):Add Single Entity
Add New Entity using DBContext in Disconnected Scenario: In this chapter you will learn how to add n ...
- Java50道经典习题-程序47 输入数字打印星号
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*. import java.util.Scanner; public class Prog47{ public static ...
- javascript使用ajax方式
ajax请回和回应实例: function showContent(type) { //create obj var xmlhttp; if (window.XMLHttpRequest) { //c ...
- Linq 和 Lambda 查询中按照多个值进行分组GroupBy
创建要查询的对象: class Employee { public int ID { get;set; } public string FName { get; set; } public int A ...