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

边栏推荐
- UVa 1025 - A Spy in the Metro (White Book)
- 21天打卡挑战学习MySQL——《Window下安装MySql》第一周 第三篇
- Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
- JPA Native Query(本地查询)及查询结果转换
- Causes of Mysql Disk Holes and Several Ways to Rebuild Tables
- 什么是memoization,它有什么用?
- 2019年10月SQL注入的两倍
- Golang Chapter 1: Getting Started
- What is Adobe?
- RPA power business automation super order!
猜你喜欢

LabVIEW代码生成错误 61056

Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo

noip初赛

七夕快乐!

关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析

嵌入式系统:时钟

藏宝计划TreasureProject(TPC)系统模式开发技术原理

如何设计 DAO 的 PoW 评判标准 并平衡不可能三角

封装、包、访问权限修饰符、static变量

node连接mysql数据库报错:Client does not support authentication protocol requested by server
随机推荐
mysql如何将表结构导出到excel
for loop exercises
pikachu Over permission
node连接mysql数据库报错:Client does not support authentication protocol requested by server
Basic Concepts of Graphs
斩获双奖|易知微荣获“2021中国数字孪生解决方案优秀供应商”“中国智能制造优秀推荐产品”双奖项!
Makefile
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
剑指offer第22题-链表中倒数第K个节点
亿流量大考(2):开发一套高容错分布式系统
21天打卡挑战学习MySQL——《Window下安装MySql》第一周 第三篇
488. Zuma Game
投资性大于游戏性 NFT游戏到底是不是门好生意
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.
举一个 web worker 的例子
Cloud platform construction solutions
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication
log4j-slf4j-impl cannot be present with log4j-to-slf4j
Testng listener