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.由 ...
随机推荐
- OpenStack Train版-14.安装块存储服务cinder(存储节点)
安装cindoer块存储服务节点(存储节点192.168.0.40)使用默认的LVM卷方法,之后改为ceph存储 安装LVM软件包 [root@cinder01 ~]# yum install lvm ...
- Kubernets二进制安装(11)之部署Node节点服务的kubelet
集群规划 主机名 角色 IP地址 mfyxw30.mfyxw.com kubelet 192.168.80.30 mfyxw40.mfyxw.com kubelet 192.168.80.40 注意: ...
- 缓冲区溢出实验 1 strcpy
实验代码 https://github.com/TouwaErioH/security/tree/master/stack%20overflow 实验目的 Buffer over flow 漏洞利用实 ...
- spring-cloud-netflix-config
Spring Cloud Config 在我们了解spring cloud config之前,我可以想想一个配置中心提供的核心功能应该有什么 提供服务端和客户端支持 集中管理各环境的配置文件 配置文件 ...
- Spring(一)概述
Spring 的前世今生 相信经历过不使用框架开发 Web 项目的 70 后.80 后都会有如此感触,如今的程序员开发项目太轻松 了,基本只需要关心业务如何实现,通用技术问题只需要集成框架便可.早在 ...
- js 深入原理讲解系列-Promise
js 深入原理讲解系列-Promise 能看懂这一题你就掌握了 js Promise 的核心原理 不要专业的术语,说人话,讲明白! Q: 输出下面 console.log 的正确的顺序? const ...
- iPhone 12 导入通讯录排序 Bug
iPhone 12 导入通讯录排序 Bug iOS iOS 通讯录排序问题 Huawei OK solution iOS 切换中英文,修复排序通讯录 bug Awesome iOS Contacts ...
- IM SDK & websocket & chart room
IM SDK & websocket & chart room IM SDK https://imsdk.com/ https://cloud.tencent.com/document ...
- flex layout & demos
flex layout & demos https://codepen.io/xgqfrms/pen/jjLPKN https://css-tricks.com/snippets/css/a- ...
- infinite auto load more & infinite scroll & load more
infinite auto load more & infinite scroll & load more https://codepen.io/xgqfrms/pen/NZVvGM ...