PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一
Pre- and Post-order Traversals
PAT-1119
- 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树
- 一篇好的文章: 题解
import java.util.Scanner;
/**
* @Author WaleGarrett
* @Date 2020/9/5 18:04
*/
public class PAT_1119 {
static int[] preorder;
static int[] postorder;
static boolean flag=true;//判断二叉树是否唯一
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
preorder=new int[n];
postorder=new int[n];
for(int i=0;i<n;i++){
preorder[i]=scanner.nextInt();
}
for(int i=0;i<n;i++){
postorder[i]=scanner.nextInt();
}
PPNode root=buildTree(0,n-1,0,n-1);
if(flag){
System.out.println("Yes");
}else System.out.println("No");
System.out.println(inOrder(root,"").trim());
}
public static PPNode buildTree(int prel,int prer,int postl,int postr){
PPNode root=new PPNode();
root.value=preorder[prel];
if(prel>=prer)
return root;
// System.out.println(prel+" "+prer);
int position=0;
if(prel<prer&&preorder[prel+1]==postorder[postr-1]){
flag=false;//不唯一
//默认为左子树
root.left=buildTree(prel+1,prer,postl,postr-1);
}else{
for(int i=postl;i<postr;i++){
// System.out.println(i+" "+(prel+1));
if(postorder[i]==preorder[prel+1]){
position=i;
break;
}
}
int numl=position-postl+1;
root.left=buildTree(prel+1,prel+numl,postl,position);
root.right=buildTree(prel+numl+1,prer,position+1,postr-1);
}
return root;
}
public static String inOrder(PPNode root,String result){
if(root.left!=null)
result=inOrder(root.left,result);
result=result+root.value+" ";
if(root.right!=null){
result=inOrder(root.right,result);
}
return result;
}
}
class PPNode{
PPNode left;
PPNode right;
int value;
public PPNode(){
left=right=null;
value=-1;
}
}
PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一的更多相关文章
- [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 ...
- [二叉树建树]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 po ...
- 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树
出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所 ...
- LeetCode OJ: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 ...
- PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题
1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a bina ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...
- 剑指offer面试题:输入某二叉树的前序遍历和中序遍历,输出后序遍历
二叉树的先序,中序,后序如何遍历,不在此多说了.直接看题目描述吧(题目摘自九度oj剑指offer面试题6): 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结 ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接:http://poj.org/problem?id=1240 本文链接:http://www.cnblogs.com/Ash-ly/p/5482520.html 题意: 通过一棵二叉树的中序 ...
- 二叉树:前序遍历、中序遍历、后序遍历,BFS,DFS
1.定义 一棵二叉树由根结点.左子树和右子树三部分组成,若规定 D.L.R 分别代表遍历根结点.遍历左子树.遍历右子树,则二叉树的遍历方式有 6 种:DLR.DRL.LDR.LRD.RDL.RLD.由 ...
随机推荐
- 【noi 2.7_413】Calling Extraterrestrial Intelligence Again(算法效率--线性筛素数+二分+测时)
题意:给3个数M,A,B,求两个质数P,Q.使其满足P*Q<=M且A/B<=P/Q<=1,并使P*Q最大.输入若干行以0,0,0结尾. 解法:先线性筛出素数表,再枚举出P,二分出对应 ...
- CentOS 7 升级内核版本
1.查看当前内核版本 $ uname -r 3.10.0-514.el7.x86_64 $ uname -a Linux k8s-master 3.10.0-514.el7.x86_64 #1 SMP ...
- C# 类(9) - 接口 Interface
Interface 接口 类似 抽象类,也不能被实例化...(前面说的静态类,加上抽象类,还有这个,都3个了)接口其实比 抽象类 更加抽象.接口的方法(这个方法还不能有实体代码,和抽象类的抽象方法差不 ...
- spring-cloud-sleuth/zipkin
Spring Cloud Sleuth 一般的,一个分布式服务跟踪系统,主要有三部分:数据收集.数据存储和数据展示.根据系统大小不同,每一部分的结构又有一定变化.譬如,对于大规模分布式系统,数据存储可 ...
- μC/OS-III---I笔记6---互斥信号量
互斥信号量 操作系统中利用信号量解决进程间的同步和互斥(互斥信号量)的问题,在多道程序环境下,操作系统就是遮掩实现进程之间的同步和互斥.但是在使用的过程中厉害的前辈还是发现了这一优秀机制的缺陷,它会导 ...
- ECMAScript 7 (ES 2016 /ES7 ) Ecma-262 7Edition
Standard ECMA-262 ECMAScript 2016 Language Specification 7th edition (June 2016) http://www.ecma-int ...
- 使用 js 实现一个简易版的动画库
使用 js 实现一个简易版的动画库 具有挑战性的前端面试题 animation css refs https://www.infoq.cn/article/0NUjpxGrqRX6Ss01BLLE x ...
- Internationalization API & ECMA-402
Internationalization API & ECMA-402 i18n https://caniuse.com/?search=Internationalization API In ...
- c++ 去掉字符串首尾空格
http://www.cplusplus.com/reference/regex/regex_replace/ #include <iostream> #include <regex ...
- js 触发长按事件
为网站添加触摸功能 <button id="btn1">长按触发</button> <button id="btn2">长按 ...