当前位置:网站首页>二叉搜索树解决落叶问题
二叉搜索树解决落叶问题
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;
}六 测试
绿色为输入,白色为输出。

边栏推荐
- 易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
- Makefile
- Why do we need callbacks
- Conditional Statements for Shell Programming
- Take an example of a web worker
- Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
- 【bug】汇总Elipse项目中代码中文乱码解决方法!
- pikachu Over permission 越权
- On the Qixi Festival of 2022, I will offer 7 exquisite confession codes, and at the same time teach you to quickly change the source code for your own use
- Storage engine written by golang, based on b+ tree, mmap
猜你喜欢
Causes of Mysql Disk Holes and Several Ways to Rebuild Tables

Data_web(八)mysql增量同步到mongodb

On the Qixi Festival of 2022, I will offer 7 exquisite confession codes, and at the same time teach you to quickly change the source code for your own use

Cisco ike2 IPSec configuration

获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》

Zilliz 2023 秋季校园招聘正式启动!

HCIP BGP lab report
![[b01lers2020]Life on Mars](/img/d0/d5c9b7224542c8843ce29adc7ef713.png)
[b01lers2020]Life on Mars

投资性大于游戏性 NFT游戏到底是不是门好生意

Bytebase数据库 Schema 变更管理工具
随机推荐
L2-041 插松枝
Take an example of a web worker
Go开发工具GoLand V2022.2 来了——Go 工作区重大升级
Data_web(八)mysql增量同步到mongodb
Golang Chapter 1: Getting Started
Canvas App中点击图标生成PDF并保存到Dataverse中
网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)
什么是memoization,它有什么用?
LabVIEW代码生成错误 61056
Golang第二章:程序结构
伴随着元宇宙、web3.0等概念的兴起,数字人、数字场景等诸多数字化的形态开始出现
Data_web(九)mongodb增量同步到mongodb
生成器版和查看器版有什么区别?
电商秒杀系统
目标检测技术研究现状及发展趋势
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
2019年10月SQL注入的两倍
封装、包、访问权限修饰符、static变量
Testng listener