当前位置:网站首页>二叉搜索树解决落叶问题
二叉搜索树解决落叶问题
2022-08-03 22:41:00 【chengqiuming】
一 原问题链接
Falling Leaves - POJ 1577 - Virtual Judgehttps://vjudge.net/problem/POJ-1577
二 输入和输出
1 输入
输入包含多个测试用例。每个测试用例都是一行或多行大写字母序列,每行都给出按上述步骤从二叉搜索树中删除的叶子,每行给出的字母都按字母升序排列。在测试用例之间以一行分隔,该行仅包含一个星号,在最后一个测试用例后给出一行,该行仅给出一个美元符号。在输入中没有空格或空行。
2 输出
对于每个测试用例,都有唯一的二叉搜索树,单行输出该树的先序遍历。
三 输入和输出样例
1 输入样例
BDHPY
CM
GQ
K
*
AC
B
$
2 输出样例
KGCBDHQMPY
BAC
四 算法设计
最后一个字母一定为树根,先输入的字母在树的深层,可以逆序建树。读入字母序列后用字符串存储,然后逆序创建二叉搜索树,将小的字母插入左子树,将大的字母插入右子树。输出该树的先序遍历:根,左子树,右子树。
五 代码
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;
} else
insert(tree[t].l, ch);
}
if (ch > tree[t].c) {
if (tree[t].r == 0) {
tree[++cnt].c = ch;
tree[t].r = cnt;
} else
insert(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;
}六 测试
绿色为输入,白色为输出。

边栏推荐
- 七夕快乐!
- HDU 5655 CA Loves Stick
- HCIP BGP实验报告
- 生成器版和查看器版有什么区别?
- 老板:公司系统太多,能不能实现账号互通?
- log4j-slf4j-impl cannot be present with log4j-to-slf4j
- 【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。
- With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
- UVa 10003 - Cutting Sticks(白书,区间DP)
- Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
猜你喜欢

LabVIEW code generation error 61056

Network basic learning series four (network layer, data link layer and some other important protocols or technologies)

Go开发工具GoLand V2022.2 来了——Go 工作区重大升级

授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节试读版

"Digital Economy Panorama White Paper" Financial Digital User Chapter released!

Embedded Systems: GPIO

Cisco ike2 IPSec配置

What is the difference between the generator version and the viewer version?

21天打卡挑战学习MySQL——《MySQL工具的使用》第一周 第二篇

Embedded Systems: Clocks
随机推荐
剑指offer第22题-链表中倒数第K个节点
Click the icon in Canvas App to generate PDF and save it to Dataverse
Nine ways to teach you to read the file path in the resources directory
Golang Chapter 1: Getting Started
UVa 437 - The Tower of Babylon(白书)
[b01lers2020]Life on Mars
【MySQL进阶】数据库与表的创建和管理
[MySQL Advanced] Creation and Management of Databases and Tables
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
encapsulation, package, access modifier, static variable
Websocket multi-threaded sending message error TEXT_PARTIAL_WRITING--Use case of spin lock replacing synchronized exclusive lock
封装、包、访问权限修饰符、static变量
With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
start with connect by implements recursive query
Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
【day1】
Cisco ike2 IPSec configuration
log4j-slf4j-impl cannot be present with log4j-to-slf4j
113. 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节