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 #648 (Div. 2) E. Maximum Subsequence Value(鸽巢原理)
题目链接:https://codeforces.com/problemset/problem/1365/E 题意 有 $n$ 个元素,定义大小为 $k$ 的集合值为 $\sum2^i$,其中,若集合内 ...
- Panasonic Programming Contest (AtCoder Beginner Contest 186) E.Throne (数学,线性同余方程)
题意:有围着一圈的\(N\)把椅子,其中有一个是冠位,你在离冠位顺时针\(S\)把椅子的位置,你每次可以顺时针走\(K\)个椅子,问最少要走多少次才能登上冠位,或者走不到冠位. 题解:这题和洛谷那个青 ...
- Java基础(第一期)
Java基础 1.注释 Java中注释有三种: 单行注释 // 多行注释 /* */ 文本注释(用的较少) /** */ 书写注释是一个非常好的习惯 BAT 平时写代码一定要注意规范 //有趣的代码注 ...
- 实战交付一套dubbo微服务到k8s集群(8)之configmap使用
使用ConfigMap管理应用配置 拆分环境 主机名 角色 IP地址 mfyxw10.mfyxw.com zk1.od.com(Test环境) 192.168.80.10 mfyxw20.mfyxw. ...
- 牛客网-Beauty of Trees 【加权并查集】
锟斤拷锟接o拷https://www.nowcoder.com/acm/contest/119/A锟斤拷源锟斤拷牛锟斤拷锟斤拷 锟斤拷目锟斤拷锟斤拷 It锟斤拷s universally acknow ...
- 6. Connection has already been closed 数据库连接被关闭
生产上Tomcat出现 Connection has already been closed.问题,但是在uat测试是好的! 遇见两次: 1.某个程序dao中执行逻辑异常复杂,有时候需要执行一分多钟, ...
- 谈一谈phar 反序列化
前言 来自Secarma的安全研究员Sam Thomas发现了一种新的漏洞利用方式,可以在不使用php函数unserialize()的前提下,引起严重的php对象注入漏洞.这个新的攻击方式被他公开在了 ...
- xss 之herf输出
首先查看下漏洞页面,发现输入的1111, 直接传参到herf 中, 查阅资料得知: 输出出现在a标签的href属性里面,可以使用javascript协议来执行js 查看源代码: if(isset($ ...
- online QRcode generator , QRcode=== (Quick Response Code) , 二维条码,二维码,彩色二维码,图片二维码,
online QRcode generator , QRcode=== (Quick Response Code) , 二维条码,二维码,彩色二维码,图片二维码, 1 http://cli.i ...
- npm publish & 403 Forbidden
npm publish & 403 Forbidden 403 Forbidden - PUT https://registry.npmjs.org/ https://www.npmjs.co ...