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 ...
随机推荐
- 【noi 2.2_1751】分解因数(递归)
题意:问一个给定正整数的分解因数的方式数.N=a1*a2*...*ak(a1<=a2<=...<=ak). 解法:一步步分解该数,总方式数为一个个因数被分解的方案数之和. 可用大括号 ...
- Codeforces Round #656 (Div. 3) B. Restore the Permutation by Merger (模拟)
题意:有两个完全相同的排列,将其中一个的元素按相对顺序插入另外一个排列中,给你操作完的排列,求原排列. 题解:感觉看看样例就能直接写了啊,直接遍历,用桶存数字个数,如果桶为空,直接输出即可. 代码: ...
- 记一次Python调试问题
C#调用python脚本错误: 使用cmd直接运行脚本报错: TabError: inconsistent use of tabs and spaces in indentation 查看日志报错: ...
- C# 异常重试策略
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- EFCore学习记录--数据访问技术人门2
1 code fist 1.创建实体类: 2.创建DbContext类: mysql连接字符串是:Server=127.0.0.1;Port=3306;Database=BlogDb; User=ro ...
- WPF 只读集合在 XAML 中的绑定(WPF:Binding for readonly collection in xaml)
问题背景 某一天,我想做一个签到打卡的日历.基于 Calendar,想实现这个目标,于是找到了它的 SelectedDates 属性,用于标记签到过的日期. 问题来了. 基于MVVM模式,想将其在xa ...
- 国产网络损伤仪 SandStorm -- 只需要拖拽就能删除链路规则
国产网络损伤仪SandStorm可以模拟出带宽限制.时延.时延抖动.丢包.乱序.重复报文.误码.拥塞等网络状况,在实验室条件下准确可靠地测试出网络应用在真实网络环境中的性能,以帮助应用程序在上线部署前 ...
- SSH服务连接
SSH基本概述 SSH是一个安全协议,在进行数据传输时,会对数据包进行加密处理,加密后在进行数据传输.确保了数据传输安全. SSH服务 ssh: secure shell, protocol, 22/ ...
- Web安全之SQL注入(原理,绕过,防御)
首先了解下Mysql表结构 mysql内置的information_schema数据库中有三个表非常重要1 schemata:表里包含所有数据库的名字2 tables:表里包含所有数据库的所有的表,默 ...
- 使用dotNET_Reactor4.7加密后的dll在VS2010中无法打包
1.只要去除加密工具中的反编译选项"Anti ILDASM",再加密就OK了. 2.或者使用VS2008打包也行.