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 ...
随机推荐
- CF1466-D. 13th Labour of Heracles
CF1466-D. 13th Labour of Heracles 题意: 给出一个由\(n\)个点构成的树,每个点都有一个权值.现在你可以用\(k,k\subset\)\([1, n]\)个颜色来给 ...
- Nginx 动静分离概述
目录 Nginx 动静分离是什么 Nginx 动静分离配置 Nginx 动静分离是什么 Nginx 动静分离,指的是静态资源请求由 Nginx 处理,动态资源请求由 php-fpm 处理或 tomca ...
- 设计模式(二十三)——策略模式(Arrays源码分析)
1 编写鸭子项目,具体要求如下: 1) 有各种鸭子(比如 野鸭.北京鸭.水鸭等, 鸭子有各种行为,比如 叫.飞行等) 2) 显示鸭子的信息 2 传统方案解决鸭子问题的分析和代码实现 1) 传统的设计方 ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- 项目管理工具看板 All In One
项目管理工具看板 All In One Trello https://trello.com/ 我们来总结一下 Trello 是 Atlassian 旗下公司,所有使用 Trello 的人都将使用 At ...
- TypeScript Developer Roadmap
TypeScript Developer Roadmap https://github.com/xgqfrms/TypeScript-Developer-Roadmap https://typescr ...
- Lua 从入门到放弃
Lua 从入门到放弃 What is Lua? Lua is a powerful, efficient, lightweight, embeddable scripting language. It ...
- npm & app-node-env
npm & app-node-env $ npm i -g app-node-env # OR $ yarn global add app-node-env demo $ ane env=ap ...
- npm & cli & node.js
npm & cli & node.js https://www.npmjs.com/ https://www.npmjs.com/settings/xgqfrms/packages h ...
- 线上 S1 故障是什么, 线上 S1 故障, 运维故障分级, 运维, 故障分级, P1 级别故障, 故障, P1 , S1
线上 S1 故障是什么 线上 S1 故障, 运维故障分级, 运维, 故障分级, P1 级别故障, 故障, P1 , S1 故障复盘 https://time.geekbang.org/column/a ...