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实现+二叉排序树的中序遍历和层次遍历的更多相关文章

  1. 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 ...

  2. PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  3. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  4. pat 甲级 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  5. 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 ...

  6. 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 ...

  7. PAT 甲级 1099 Build A Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...

  8. 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 ...

  9. 1099. Build A Binary Search Tree (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  10. 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 ...

随机推荐

  1. Codeforces Round #646 (Div. 2) E. Tree Shuffling(树上dp)

    题目链接:https://codeforces.com/contest/1363/problem/E 题意 有一棵 $n$ 个结点,根为结点 $1$ 的树,每个结点有一个选取代价 $a_i$,当前 $ ...

  2. zjnu1707 TOPOVI (map+模拟)

    Description Mirko is a huge fan of chess and programming, but typical chess soon became boring for h ...

  3. 【noi 2.6_9267】核电站(DP)

    题意:n个数中不能同时选连续m个或以上,问方案数. 解法:f[i][j]表示从前i个中选,到第i个已经连续选了j个.j!=0时,  =f[i-1][j-1] ; j=0时, =f[i-1][0~m-1 ...

  4. zoj3623 Battle Ships

    Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense ...

  5. Python Base64编码解码

    import base64 str = '12345678'.encode('utf8') print(base64.b64encode(str).decode('utf8')) # 编码 接收的参数 ...

  6. 男孩周末班-k8s-思维导图

  7. 【非原创】codeforces - 1067A Array Without Local Maximums【dp】

    学习博客:戳这里 附本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 co ...

  8. 深入理解JavaScript中的类继承

    由于写本文时全部是在编辑器中边写代码边写感想的,所以,全部思想都写在代码注释里面了 // 类继承 //todo.1 extends 关键字 class Animal { constructor(nam ...

  9. μC/OS-III---I笔记11---就绪任务列表管理

    就绪优先级为映像响表 在UCOSIII内,任务调度是要先找到优先级最高的任务,然后执行.理论上对于UCOSIII可以有无数个优先级,每个优先级又可以有无数个任务但是对于这么多的任务如何快速查到到当先就 ...

  10. PyCharm 中文 字符 python 报错 的 完美 解决方案!

    PyCharm 中文 字符 python 报错 的 完美 解决方案! #_*_ coding:utf-8_*_ https://www.python.org/dev/peps/pep-0263/ 到p ...