当前位置:网站首页>binary search tree problem
binary search tree problem
2022-08-05 07:04:00 【Chengqiuming】
An original problem description
Binary Search Tree - HDU 3791 - Virtual Judgehttps://vjudge.net/problem/HDU-3791
Two Inputs and Outputs
1 input
Start a number n, (1<=n<=20) means that there are n numbers to be judged, and the input ends when n=0.
The next line is a sequence, the length of the sequence is less than 10, contains numbers (0~9), and there are no repeated numbers. According to this sequence, a binary search tree can be constructed.
There are n sequences in the next n lines. The format of each sequence is the same as the first sequence. Please judge whether the two sequences can form the same binary search tree.
2 output
If the sequence is the same, output YES, otherwise output NO
Three input and output samples
1 Input example
2
567432
543267
576342
0
2 Sample output
YES
NO
Three Analysis
Inorder traversal and preorder traversal of a tree can uniquely determine a binary tree.Therefore, a binary search tree can be constructed first. At this time, the in-order traversal is the same. If the pre-order traversal is the same, it is the same binary search tree.
Four Algorithms Design
1 Using a binary search tree, first store each number in the binary search tree to get a preorder traversal.
2 Store each number in the following line into the binary search tree, get pre-order traversal, compare whether they are equal, if they are equal, output "YES", otherwise output "NO".
Five codes
package hdu3791;import java.util.Scanner;public class HDU3791 {static int cnt;static String a; // input stringstatic char b[] = new char[15]; // original sequence preorder sequencestatic char c[] = new char[15]; // preorder sequence for other sequencesstatic Node root;static Node insert(Node root, int num) { //Insert x into binary search tree tif (root == null) {// if the tree is emptyreturn new Node(num);}// A temporary node points to the root node for the return valueNode tmp = root;Node pre = root;while (root != null && root.num != num) {// save the parent nodepre = root;if (num > root.num) {root = root.rc;} else {root = root.lc;}}// add via parent nodeif (num > pre.num) {pre.rc = new Node(num);} else {pre.lc = new Node(num);}return tmp;}static void preorder(Node t, char b[]) {//Inorder traversalif (t != null) {b[cnt++] = (char) (t.num + '0');preorder(t.lc, b);preorder(t.rc, b);}}public static void main(String[] args) {int n;Scanner scanner = new Scanner(System.in);while (true) {n = scanner.nextInt();if (n == 0) {return;}cnt = 0;root = null;a = scanner.next();for (int i = 0; i < a.length(); i++)root = insert(root, a.charAt(i) - '0');preorder(root, b);b[cnt] = '\0';while (n--> 0) {cnt = 0;root = null;a = scanner.next();for (int i = 0; i < a.length(); i++)root = insert(root, a.charAt(i) - '0');preorder(root, c);c[cnt] = '\0';String sb = new String(b);String sc = new String(c);if (sb.equals(sc))System.out.println("YES");elseSystem.out.println("NO");}}}}class Node {int num;Node lc;Node rc;Node(int num) {this.num = num;}}
Six Tests
边栏推荐
- MySql面试题总结
- 【instancetype类型 Objective-C】
- Day9 of Hegong Daqiong team vision team training - camera calibration
- PCI Pharma Services Announces Multi-Million Dollar Expansion of UK Manufacturing Facility to Meet Growing Demand for Global High Potency Drug Manufacturing Services to Support Oncology Treatment
- Promise (3) async/await
- 17-VMware Horizon 2203 virtual desktop-Win10 manual desktop pool floating (seventeen)
- 原来使Maya Arnold也能渲染出高质量作品!超赞小技巧
- Rapid Medical's Ultra-Small and Only Adjustable Thromb Retriever Receives FDA Clearance
- 浮点数基础知识
- LabVIEW中如何实现任意形状的不规则按键
猜你喜欢
随机推荐
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
AI+视频技术助力保障校园安全,校园智能安防平台该如何建设?
淘宝宝贝页面制作
Hong Kong International Jewellery Show and Hong Kong International Diamond, Gem and Pearl Show kick off
AH8669-AC380/VAC220V转降5V12V24V500MA内电源芯片IC方案
【MyCat简单介绍】
Rapid Medical超小体积且唯一可调的取栓器获得FDA核准
Matplotlib plotting notes
白鹭egret添加新页面教程,如何添加新页面
ndk编译so库
八大排序之快速排序
技术分析模式(九)三重顶部和底部
PCI Pharma Services Announces Multi-Million Dollar Expansion of UK Manufacturing Facility to Meet Growing Demand for Global High Potency Drug Manufacturing Services to Support Oncology Treatment
一天学会从抓包到接口测试,通过智慧物业项目深度解析
Mysql为什么 建立数据库失败
HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
Redis的使用
合工大苍穹战队视觉组培训Day9——相机标定
浮点数基础知识
关于Antd的Affix突然不好用了,或者Window的scroll监听不好用了