• 二叉搜索树问题


    一 原问题描述

    二叉搜索树 - HDU 3791 - Virtual Judgehttps://vjudge.net/problem/HDU-3791

    二 输入和输出

    1 输入

    开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。

    接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。

    接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。

    2 输出

    如果序列相同则输出YES,否则输出NO

    三 输入和输出样例

    1 输入样例

    2

    567432

    543267

    576342

    0

    2 输出样例

    YES

    NO

    三 分析

    一棵树的中序遍历和先序遍历可以唯一确定一棵二叉树。所以可以先构造一棵二叉搜索树,此时中序遍历一样,如果先序遍历一样,则是同一棵二叉搜索树。

    四 算法设计

    1 使用二叉搜索树,先将每个数字都存进二叉搜索树,得到先序遍历。

    2 将后面每一行的每个数字都存进二叉搜索树中,得到先序遍历,比较其是否相等,如果相等,则输出"YES",否则输出"NO"。

    五 代码

    1. package hdu3791;
    2. import java.util.Scanner;
    3. public class HDU3791 {
    4. static int cnt;
    5. static String a; // 输入字符串
    6. static char b[] = new char[15]; // 原始序列先序序列
    7. static char c[] = new char[15]; // 其他序列的先序序列
    8. static Node root;
    9. static Node insert(Node root, int num) { //将x插入到二叉搜索树t中
    10. if (root == null) {
    11. // 树为空树的情况
    12. return new Node(num);
    13. }
    14. // 一个临时节点指向根节点,用于返回值
    15. Node tmp = root;
    16. Node pre = root;
    17. while (root != null && root.num != num) {
    18. // 保存父节点
    19. pre = root;
    20. if (num > root.num) {
    21. root = root.rc;
    22. } else {
    23. root = root.lc;
    24. }
    25. }
    26. // 通过父节点添加
    27. if (num > pre.num) {
    28. pre.rc = new Node(num);
    29. } else {
    30. pre.lc = new Node(num);
    31. }
    32. return tmp;
    33. }
    34. static void preorder(Node t, char b[]) {//中序遍历
    35. if (t != null) {
    36. b[cnt++] = (char) (t.num + '0');
    37. preorder(t.lc, b);
    38. preorder(t.rc, b);
    39. }
    40. }
    41. public static void main(String[] args) {
    42. int n;
    43. Scanner scanner = new Scanner(System.in);
    44. while (true) {
    45. n = scanner.nextInt();
    46. if (n == 0) {
    47. return;
    48. }
    49. cnt = 0;
    50. root = null;
    51. a = scanner.next();
    52. for (int i = 0; i < a.length(); i++)
    53. root = insert(root, a.charAt(i) - '0');
    54. preorder(root, b);
    55. b[cnt] = '\0';
    56. while (n-- > 0) {
    57. cnt = 0;
    58. root = null;
    59. a = scanner.next();
    60. for (int i = 0; i < a.length(); i++)
    61. root = insert(root, a.charAt(i) - '0');
    62. preorder(root, c);
    63. c[cnt] = '\0';
    64. String sb = new String(b);
    65. String sc = new String(c);
    66. if (sb.equals(sc))
    67. System.out.println("YES");
    68. else
    69. System.out.println("NO");
    70. }
    71. }
    72. }
    73. }
    74. class Node {
    75. int num;
    76. Node lc;
    77. Node rc;
    78. Node(int num) {
    79. this.num = num;
    80. }
    81. }

    六 测试

     

  • 相关阅读:
    LinkedList源码分析
    java基于微信小程序的劳务知识分享系统 uniapp 小程序
    <专利>机器人3D视觉快速定位抓取方法及系统
    SpringBoot整合MQTT(MqttClient)
    C++性能分析工具gperftools安装教程与使用案例分析
    创建数组array--numpy
    h5插件_h5页面嵌入客户端调试
    pandas 构造时间值
    linux两块硬盘挂载同一个目录
    [附源码]计算机毕业设计JAVA学生量化考核管理系统
  • 原文地址:https://blog.csdn.net/chengqiuming/article/details/126166585