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: 输出完毕中序遍历后 ...
随机推荐
- XMPP 客户端和服务端
GPLv2授权不能商用 XMPP协议的客户端软件列表 http://zh.wikipedia.org/wiki/XMPP%E5%8D%94%E8%AD%B0%E7%9A%84%E5%AE%A2%E6% ...
- [Python Study Notes]折线图绘制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- CORS实现跨域Ajax
客户端 #!/usr/bin/env python import tornado.ioloop import tornado.web class MainHandler(tornado.web.Req ...
- 在异步回调中调用MessageBox.Show
public static void Test() { ThreadStart aThreadStart = delegate() { ); MessageBox.Show("Good!&q ...
- 前端学习笔记2017.6.12 CSS控制DIV
前一篇文章中用div布局了豆瓣东西的页面,如果用html代码表示的话大概是这个样子的 <!DOCTYPE html><html><head></head> ...
- parseXXX的用法
转换字符串. parseXXX是Integer类.等基本数据类型包装类的方法,用于实现String和int型数据的转换.例如, Integer.getInteger(String s) 从字符串中获取 ...
- R: plot 绘图各种参数,以及 legend。。
################################################### 问题:基础绘图plot 18.4.30 plot函数,基础绘图的各个参数? 解决方案: ca ...
- 22、linux的ssh互信配置
转载:https://blog.csdn.net/hrn1216/article/details/51568830 https://blog.csdn.net/u013144287/article/d ...
- 安装visual_Paradigm,时序图的应用
此安装包已经上传到sunny的百度网盘. 删除,即,右击别的地方,然后,选择delete即可. 拖箭头,拖到某个实体上,再松开,会自动连线. 很好的一款画图工具.
- java IO的总结
1: fileChannel 没有bufferedreader快, bufferedreader 可设置缓冲大小和编码 2: bufferedreader 的readline 遇到回车也换行