[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)
1119. Pre- and Post-order Traversals (30)
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
分析:目前,关于二叉树的遍历算法,有前序和中序,后序和中序,层序和中序,这三种组合都是可以唯一确定一颗二叉树的,目前都有用代码实现。其中,前序和中序建树和后序和中序建树两种方法都是想办法分出左右子树,然后对左右字数进行递归建树。层序和中序也可以用递归实现,但是目前我只用了非递归的实现方法。关于今天的建树方法,使用的是二叉树的前序遍历和后序遍历。但是我们知道,仅有前序遍历和后序遍历是没法确定一颗二叉树的。这里,前序遍历和后序遍历在某些调剂下可以唯一确定一颗二叉树。但这里不做这个要求。题目的要求是当产生歧义是可随意建立一颗满足前序和后序遍历的二叉树即可。
目前已经碰到了二叉树建树的大多数情况,下次有时间稍微总结一下。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; vector<int > pre,post,in; struct Node
{
int data;
Node *l,*r;
}; bool flag=true; void creat(Node * & root,int preL,int preR,int postL,int postR)
{
if(preL==preR)
{
root=new Node;
root->data=pre[preL];
root->l=root->r=NULL;
return ;
}
root=new Node;
root->data=pre[preL];
root->l=root->r=NULL;
int i,j;
for(i=postL;i<=postR;i++)
{
if(post[i]==pre[preL+])
{
break;
}
}
int leftNum=i-postL;
if(post[i]==post[postR-])//不确定的条件,无法区分是左子树还是右子树
{
flag=;
creat(root->l,preL+,preR,postL,postR-);
}
else
{
creat(root->l,preL+,preL++leftNum,postL,postL+leftNum);
creat(root->r,preL+leftNum+,preR,postL+leftNum+,postR-);
}
} vector<int> ans; void inOrder(Node * root)
{
if(root!=NULL)
{
inOrder(root->l);
ans.push_back(root->data);
inOrder(root->r);
}
} void outans()
{
for(int i=;i<ans.size();i++)
{
if(i>) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int k;
scanf("%d",&k);
pre.push_back(k);
}
for(int i=;i<n;i++)
{
int k;
scanf("%d",&k);
post.push_back(k);
}
Node * root=NULL;
creat(root,,n-,,n-);
inOrder(root);
if(flag==true) printf("Yes\n");
else printf("No\n");
outans();
return ;
}
[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)的更多相关文章
- PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一
Pre- and Post-order Traversals PAT-1119 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树 一篇好的文章: 题解 import java. ...
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树
出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所 ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- n进制转十进制
#include<cstdio> #include<iostream> using namespace std; ; int main(){ ,len=; char ch[ma ...
- springMVC第一天——入门、整合与参数绑定
大纲摘要: 1.Springmvc介绍 2.入门程序 3.Springmvc架构讲解 a) 框架结构 b) 组件说明 4.Springmvc整合mybatis 5.参数绑定 乱码问题解决 a) Spr ...
- tkinter的GUI设计:界面与逻辑分离(一)-- 初次见面
PyQt实现界面与逻辑分离的设计很是方便,详情可以见我的第一篇博文. 不过本文将使用python的标准库 tkinter,来实现界面与逻辑分离的GUI 设计. 我们来设计一个很简单的程序: 目的:长度 ...
- 8-[表操作]--foreign key、表与表的关系
1. foreign key (1)快速理解foreign key 员工信息表有三个字段:工号 姓名 部门 公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费 ...
- Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
- cjoj P1435 - 【模板题 USACO】AC自动机 && 洛谷 P3796 【模板】AC自动机(加强版)
又打了一遍AC自动稽. 海星. 好像是第一次打trie图,很久以前就听闻这个思想了.OrzYYB~ // It is made by XZZ #include<cstdio> #inclu ...
- Python之元类详细解析
一.补充内置函数isinstance和issubclass 1.isinstance是判断一个对象是不是由一个对象产生的 class Foo: pass obj=Foo() print(isinsta ...
- nginx Location正则表达式
1. Location正则表达式 1.1. location的作用 location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作. ...
- 域名配置https
阿里可以一年的免费申请https证书 (1)域名->管理->免费开启SSL证书 (2)申请完.等待审核后就可以下载证书压缩包,包括key和pem两个文件 (3)在服务器的nginx目录下创 ...
- Python之面向对象-反射
一.什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问,检测和修改它本省状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...