当前位置:网站首页>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.
边栏推荐
猜你喜欢
PowerMockup 4.3.4::::Crack
Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
Shell编程的条件语句
Pytest学习-setup/teardown
直播预告 | 构建业务智联,快速拥抱财务数字化转型
MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
Cisco ike2 IPSec配置
Republish the lab report
Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
Flutter 桌面探索 | 自定义可拖拽导航栏
随机推荐
互联网用户账号信息管理规定今起施行:必须严打账号买卖灰产
Cloud platform construction solutions
为什么我们需要回调
LabVIEW code generation error 61056
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
物联网新零售模式,引领购物新潮流
UVa 10003 - Cutting Sticks (White Book, Interval DP)
完全二叉树问题
Adobe是什么?
Golang Chapter 1: Getting Started
utlis thread pool
使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
用于流动质押和收益生成的 Web3 基础设施
举一个 web worker 的例子
Flutter 桌面探索 | 自定义可拖拽导航栏
Republish the lab report
Optimize the query (work in progress)
Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
Zilliz 2023 秋季校园招聘正式启动!
RPA power business automation super order!