当前位置:网站首页>Binary search tree to solve the fallen leaves problem
Binary search tree to solve the fallen leaves problem
2022-08-03 22:46:00 【Chengqiuming】
A link to the original question
Falling Leaves - POJ 1577 - Virtual Judgehttps://vjudge.net/problem/POJ-1577
Two Inputs and Outputs
1 input
The input contains multiple test cases.Each test case is a sequence of one or more uppercase letters, each giving the leaves removed from the binary search tree as described above, each giving letters in ascending alphabetical order.Separate the test cases with a line that contains only an asterisk, and give a line after the last test case that contains only a dollar sign.There are no spaces or blank lines in the input.
2 output
For each test case, there is a unique binary search tree, and a single line outputs the preorder traversal of the tree.
Three input and output samples
1 Input example
BDHPY
CM
GQ
K
*
AC
B
$
2 Sample output
KGCBDHQMPY
BAC
Four Algorithms Design
The last letter must be the root of the tree. The letter entered first is in the deep layer of the tree, and the tree can be built in reverse order.After reading the sequence of letters, store them as strings, and then create a binary search tree in reverse order, inserting small letters into the left subtree and inserting larger letters into the right subtree.Output the preorder traversal of the tree: root, left subtree, right subtree.
Five codes
package poj1577;import java.util.Scanner;public class Poj1577 {static int cnt = 1;static data tree[] = new data[110];static void insert(int t, char ch) {if (tree[t].c == 0) {tree[t].c = ch;return;}if (ch < tree[t].c) {if (tree[t].l == 0) {tree[++cnt].c = ch;tree[t].l = cnt;} elseinsert(tree[t].l, ch);}if (ch > tree[t].c) {if (tree[t].r == 0) {tree[++cnt].c = ch;tree[t].r = cnt;} elseinsert(tree[t].r, ch);}}static void preorder(int t) {if (tree[t].c == 0)return;System.out.print(tree[t].c);preorder(tree[t].l);preorder(tree[t].r);}public static void main(String[] args) {String s1, s;while (true) {s = "";for (int i = 0; i < tree.length; i++) {tree[i] = new data();}Scanner scanner = new Scanner(System.in);while (true) {s1 = scanner.next();if (s1.charAt(0) != '*' && s1.charAt(0) != '$') {s += s1;} else {break;}}for (int i = s.length() - 1; i >= 0; i--)insert(1, s.charAt(i));preorder(1);System.out.println();if (s1.charAt(0) == '$')break;}}}class data {int l;int r;char c;}
Six Tests
Green is input and white is output.
边栏推荐
- for loop exercises
- 直播预告 | 构建业务智联,快速拥抱财务数字化转型
- Why do we need callbacks
- start with connect by 实现递归查询
- 藏宝计划TreasureProject(TPC)系统模式开发技术原理
- [N1CTF 2018]eating_cms
- JPA Native Query(本地查询)及查询结果转换
- Cisco ike2 IPSec配置
- node连接mysql数据库报错:Client does not support authentication protocol requested by server
- Cisco ike2 IPSec configuration
猜你喜欢
Embedded Systems: Clocks
Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
【MySQL进阶】数据库与表的创建和管理
FinClip最易用的智能电视小程序
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
[MySQL Advanced] Creation and Management of Databases and Tables
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
PowerMockup 4.3.4::::Crack
用于流动质押和收益生成的 Web3 基础设施
LabVIEW代码生成错误 61056
随机推荐
log4j-slf4j-impl cannot be present with log4j-to-slf4j
UVa 10003 - Cutting Sticks(白书,区间DP)
Quickly build a website with static files
物联网新零售模式,引领购物新潮流
[b01lers2020]Life on Mars
走迷宫 BFS
静态文件快速建站
如何基于WPF写一款数据库文档管理工具(二)
藏宝计划TreasureProject(TPC)系统模式开发技术原理
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
The development status of cloud computing at home and abroad
Click the icon in Canvas App to generate PDF and save it to Dataverse
斩获双奖|易知微荣获“2021中国数字孪生解决方案优秀供应商”“中国智能制造优秀推荐产品”双奖项!
log4j-slf4j-impl cannot be present with log4j-to-slf4j
Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo
剑指offer第22题-链表中倒数第K个节点
使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
为什么我们需要回调
Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂