PAT-1043(Is It a Binary Search Tree)JAVA实现
Is It a Binary Search Tree
PAT-1043
- 主要涉及到根据前序遍历序列片段是否是一颗二叉树,这里有一个小tip就是插入序列就是二叉树的前序遍历序列。
- 第二个是会对排序二叉树进行前序遍历,后序遍历,镜像排序二叉树的前序遍历,后序遍历等操作。
- 题目的整体难度不大,但是对二叉树和二叉排序树的了解需要较深。
/**
* @Author WaleGarrett
* @Date 2020/9/5 8:20
*/
import java.util.Scanner;
/**
* PAT-1043,1064,1066,1086,1089,1099,1098
* 7
* 8 6 5 7 10 8 11
*/
public class PAT_1043 {
static final int maxn=1003;
public static void main(String[] args) {
BinaryTree binaryTree=new BinaryTree();
Scanner scanner=new Scanner(System.in);
String result="";
int n=scanner.nextInt();
while(n!=0){
int value=scanner.nextInt();
binaryTree.insert(value);//插入输入串
result=result+value+" ";
n--;
}
//使用前序遍历该构建完成的排序二叉树,并且对该树的镜像二叉树进行前序遍历,任何一个序列和题目的序列匹配则说明符合要求,再输出该排序二叉树的后序遍历
String preorder=binaryTree.preOrder(binaryTree.root,"");//前序遍历
String mpreorder=binaryTree.mPreOrder(binaryTree.root,"");//镜像前序遍历
if(preorder.equals(result)){
System.out.println("YES");
System.out.println(binaryTree.postOrder(binaryTree.root,"").trim());
}else if(mpreorder.equals(result)){
System.out.println("YES");
System.out.println(binaryTree.mPostOrder(binaryTree.root,"").trim());
}else
System.out.println("NO");
}
}
class Node{
Node left;
Node right;
int value;
public Node(){
left=right=null;
value=-1;
}
public Node(Node left,Node right,int value){
this.value=value;
this.left=left;
this.right=right;
}
}
class BinaryTree{
Node root;
public BinaryTree(){
root=null;
}
public void insert(int value){
if(root==null){
root=new Node();
root.value=value;
}else{
Node now=root;
while(true){
if(value<now.value){
if(now.left==null){
now.left=new Node(null,null,value);
break;
}else now=now.left;
}else{
if(now.right==null){
now.right=new Node(null,null,value);
break;
}else{
now=now.right;
}
}
}
}
}
public String preOrder(Node now,String result){
result=result+now.value+" ";
Node left=now.left;
if(left!=null){
result=preOrder(left,result);
}
Node right=now.right;
if(right!=null){
result=preOrder(right,result);
}
return result;
}
public String mPreOrder(Node now,String result){
result=result+now.value+" ";
Node right=now.right;
if(right!=null){
result=mPreOrder(right,result);
}
Node left=now.left;
if(left!=null){
result=mPreOrder(left,result);
}
return result;
}
public String postOrder(Node now,String result){
Node left=now.left;
if(left!=null){
result=postOrder(left,result);
}
Node right=now.right;
if(right!=null){
result=postOrder(right,result);
}
result=result+now.value+" ";
return result;
}
public String mPostOrder(Node now,String result){
Node right=now.right;
if(right!=null){
result=mPostOrder(right,result);
}
Node left=now.left;
if(left!=null){
result=mPostOrder(left,result);
}
result=result+now.value+" ";
return result;
}
}
PAT-1043(Is It a Binary Search Tree)JAVA实现的更多相关文章
- PAT 1043 Is It a Binary Search Tree[二叉树][难]
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT 1043 Is It a Binary Search Tree
#include <cstdio> #include <climits> #include <cstdlib> #include <vector> co ...
- 【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 binary ...
- 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 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT 甲级 1043 Is It a Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...
- PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- Codeforces Round #649 (Div. 2) A. XXXXX
题目链接:https://codeforces.com/contest/1364/problem/A 题意 找出大小为 $n$ 的数组 $a$ 的最长连续子数组,其元素和不被 $x$ 整除. 题解 如 ...
- 病毒侵袭持续中 HDU - 3065 AC自动机
小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒 ...
- 说说Golang goroutine并发那些事儿
摘要:今天我们一起盘点一下Golang并发那些事儿. Golang.Golang.Golang 真的够浪,今天我们一起盘点一下Golang并发那些事儿,准确来说是goroutine,关于多线程并发,咱 ...
- k8s-3-容器云监控系统
apollo小结 课程目录 一.容器云监控prometheus概述 https://prometheus.io/docs/introduction/overview/ #官方文档 https://gi ...
- L3-002. 堆栈【主席树 or 线段树 or 分块】
L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道"堆栈"是一种"先 ...
- LOJ6285 数列分块入门9(分块 区间众数)题解
题意:给出区间内的最小众数 思路:分块,离散化每个数,开vector记录每个数p出现的位置,这样就能二分出L,R以内p的个数了.众数有一个性质,用mode(a)表示集合a的众数,那么mode(a∪b) ...
- C语言中函数的调用方式
第一眼看到这样一个题目的我,心想除了如下的直接调用还能怎么调用呢? 1 void fun(void) 2 { 3 ...... 4 //你的代码 5 ..... 6 } 7 int main(void ...
- CF 1477A. Nezzar and Board
传送门 思路: 从k = 2 * x - y ==> 2 * x = k + y ,可以看出x是k,y的中间值,则如果存在x1,x2,且x1 = x2 ± 1,则通过x1,x2可以得到所有整数, ...
- 24 WAYS to impress your friends
24 WAYS to impress your friends 24 ways is the advent calendar for web geeks. For twenty-four days e ...
- HTML5 image rotate effect
HTML5 image rotate effect HTML5 实现旋转拨号键盘 简单的html中js实现图片中心旋转 https://download.csdn.net/download/q3168 ...