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.由 ...
随机推荐
- 在异步或子线程中show窗体的时候要用MethodInvoker委托,要不然show不出来
this.Invoke((MethodInvoker)delegate () { Thread.Sleep(500); this.Hide(); FloatWnd floatWnd = new Flo ...
- Pyqt5使用
一.帮助文档 二.PyQt5库结构 三. 面向对象的编程模式 class Windows(QWidget): def __init__(self): #继承父类的QWidget的方法 super(). ...
- BZOJ 3676 回文串(回文树)题解
题意: 一个回文的价值为长度 * 出现次数,问一个串中的子串的最大回文价值 思路: 回文树模板题,跑PAM,然后计算所有节点出现次数. 参考: 回文串问题的克星--Palindrome Tree(回文 ...
- Inkscape tricks
Draw straight lines: click pencil button -> click once on your canvas(starting point) -> click ...
- Android四大组件简介:Android 基础知识,开发教程
Android 四大组件: Activity.Service.Broadcast Receiver.Content Provider. http://developer.android.com/int ...
- Adobe DreamWeaver CC 快捷键
1 1 ADOBE DREAMWEAVER CC Shortcuts: DREAMWEAVER CC DOCUMENT EDITING SHORTCUTS Select Dreamweaver > ...
- js 深入原理讲解系列-Promise
js 深入原理讲解系列-Promise 能看懂这一题你就掌握了 js Promise 的核心原理 不要专业的术语,说人话,讲明白! Q: 输出下面 console.log 的正确的顺序? const ...
- HTML5 dataset All In One
HTML5 dataset All In One dataset https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignEleme ...
- Flutter Widgets
Flutter Widgets Flutter 组件 Syncfusion Flutter Widgets 所有组件均支持即装即用的 Android,iOS和 Web not free https:/ ...
- 小程序 & taro 踩坑指南
小程序 & taro 踩坑指南 微信开发者工具, 不支持 react bug https://github.com/NervJS/taro/issues/5042 solution just ...