PAT-1099(Build A Binary Search Tree)Java实现+二叉排序树的中序遍历和层次遍历
Build A Binary Search Tree
PAT-1099
- 本题有意思的一个点就是:题目已经给出了一颗排序二叉树的结构,需要根据这个结构和中序遍历序列重构一棵二叉排序树。
- 解法:可以根据中序遍历的思路,首先将给定的序列串进行排序即是中序遍历的结果。接着,根据给定的树结构进行中序遍历,这期间就可以确定每个结点的值。最后,只需要根据这棵树就能进行层次遍历了。
- 题目具备一些难度,需要一些思维能力和扩展能力。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* @Author WaleGarrett
* @Date 2020/9/5 16:20
*/
public class PAT_1099 {
static BNode[] nodes;
static int[] number;
static int cnt=0;//中序遍历的个数
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
nodes=new BNode[n];
number=new int[n];
for(int i=0;i<n;i++){
nodes[i]=new BNode();
int left=scanner.nextInt();
int right=scanner.nextInt();
nodes[i].left=left;
nodes[i].right=right;
}
for(int i=0;i<n;i++)
number[i]=scanner.nextInt();
Arrays.sort(number);
inOrder(0);
String result=levelOrder(0);
System.out.println(result.trim());
}
public static void inOrder(int n){
if(nodes[n].left!=-1){
inOrder(nodes[n].left);//进入左子树
}
nodes[n].value=number[cnt++];
if(nodes[n].right!=-1){
inOrder(nodes[n].right);//进入右子树
}
}
public static String levelOrder(int n){
String result="";
List<Integer> list=new ArrayList<>();
list.add(n);
while(list.size()!=0){
int now=list.remove(0);
int left=nodes[now].left;
int right=nodes[now].right;
result=result+nodes[now].value+" ";
if(left!=-1) list.add(left);
if(right!=-1) list.add(right);
}
return result;
}
}
class BNode{
int left,right,value;
}
PAT-1099(Build A Binary Search Tree)Java实现+二叉排序树的中序遍历和层次遍历的更多相关文章
- PAT 1099 Build A Binary Search Tree[BST性质]
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...
- PAT 甲级 1099 Build A Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- Poj-3922 A simple stone game(k倍动态减法)
题意: 游戏是这样的:两个玩家以一堆n个石头开始游戏.他们轮流从石堆里取石头,每次至少取一块.先走的人第一步最多可以拿n-1块石头.从那时起,一个玩家最多可以拿k倍于他的对手上次拿的石头.例如,如果一 ...
- 母函数 <普通母函数(HDU - 1028 ) && 指数型母函数(hdu1521)>
给出我初学时看的文章:母函数(对于初学者的最容易理解的) 普通母函数--------->HDU - 1028 例题:若有1克.2克.3克.4克的砝码各一 枚,能称出哪几种重量?各有几种可能方案? ...
- Git管理远程仓库
一:使用远程仓库的目的 作用:备份,实现代码共享集中化管理: 二:将git本地仓库同步到远程仓库流程图 三:Git克隆操作 目的: 将远程仓库(github远程仓库项目代码)克隆到本地 如何克隆 1. ...
- 流媒体传输协议之 RTP(下篇)
本系列文章将整理各个流媒体传输协议,包括 RTP/RTCP,RTMP,希望通过深入梳理协议的设计细节,能够给流媒体领域的开发者带来一定的启发. 作者:逸殊 审核:泰一 接上篇:< 流媒体传输协议 ...
- woj1008feedinganimals2-贪心-网络流
title: woj1008feedinganimals2-贪心-网络流 date: 2020-03-07 categories: acm tags: [acm,woj,网络流,贪心] 中等题. 标准 ...
- hdu-1941 Find the Shortest Common Superstring
The shortest common superstring of 2 strings S 1 and S 2 is a string S with the minimum number of ch ...
- Linux 驱动框架---设备文件devfs
设备文件系统 Linux引入了虚拟文件系统,从而使设备的访问可以像访问普通文件系统一样.因此在内核中描述打开文件的数据inode中的rdev成员用来记录设备文件对应到的设备号.设备文件也由一个对应的f ...
- online QRcode generator , QRcode=== (Quick Response Code) , 二维条码,二维码,彩色二维码,图片二维码,
online QRcode generator , QRcode=== (Quick Response Code) , 二维条码,二维码,彩色二维码,图片二维码, 1 http://cli.i ...
- 网站备案查询/ICP备案查询网
网站备案查询/ICP备案查询网 互联网站备案信息全国公安机关互联网站安全服务平台http://www.beian.gov.cn/portal/index 1 http://www.miitbeian. ...
- YouTube 视频下载工具
YouTube 视频下载工具 我不生产视频,只是优秀视频的搬运工! YouTube-dl https://github.com/search?q=youtube-dl https://github.c ...