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.由 ...
随机推荐
- .Net Core 国际化
创建项目什么的就不说了吧 直接进入正题吧 我这里建的是个webapi 添加资源文件 1.首先我们创建一个Language文件夹,这就是我们在后面Startup类中需要配置的目录名. 2.然后我们在La ...
- SSM框架整合(Spring + SpringMVC + MyBatis)
搭建环境 使用Spring(业务层)整合其他的框架SpringMVC(表现层)和MyBatis(持久层) Spring框架 创建数据库表 CREATE DATABASE ssm; USE ssm; C ...
- Docker应用场景和局限性
Docker有哪些好的特性?作为一种新兴的虚拟化方式,Docker跟传统的虚拟化方式相比具有众多的优势.首先, Docker容器的启动可以在秒级实现,这相比传统的虚拟机方式要快得多.其次, Docke ...
- vue中获取元素并控制相应的dom
1 在标签中使用ref="xxx" 2 在methods中调用this.$refs.xxx this.$refs.xxx.$el获取dom 注意1:大多数情况下为了复用方法,将xx ...
- HDU4578 Transformation(多标记线段树)题解
题意: 操作有:\(1\).区间都加\(a\):\(2\).区间都乘\(a\):\(3\).区间都重置成\(a\):\(4\).询问区间幂次和\(\sum_{i=l}^rnum[i]^p(p\in\{ ...
- POJ 2288 Islands and Bridges(状压DP)题解
题意:n个点,m有向边,w[i]表示i的价值,求价值最大的哈密顿图(只经过所有点一次).价值为:所有点的w之和,加上,每条边的价值 = w[i] * w[j],加上,如果连续的三个点相互连接的价值 = ...
- php 文件包含base64读取文件 preg_replace函数
解题部分题目来源攻防世界web高手进阶区1.拿到题目以后,发现是一个index.php的页面,并且设备-没有显示完全,此位置可疑.2.源代码中发现?page=index,出现page这个get参数,联 ...
- 惠普机械键盘 K10GL 使用评测
惠普机械键盘 GK100 使用评测 手感太差,不是 RGB 背光 惠普(HP) K10GL 机械键盘 有线 LED背光机械键盘 87键 混光青轴 refs https://item.jd.com/10 ...
- LeetCode & linked list bug
LeetCode & linked list bug add-two-numbers shit test /** * Definition for singly-linked list. * ...
- Github & DMCA Takedown & git remove history
Github & DMCA Takedown & git remove history Github & DMCA Takedown Policy Removing files ...