LeetCode 988. Smallest String Starting From Leaf
原题链接在这里:https://leetcode.com/problems/smallest-string-starting-from-leaf/
题目:
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on.
Find the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
(As a reminder, any shorter prefix of a string is lexicographically smaller: for example, "ab" is lexicographically smaller than "aba". A leaf of a node is a node that has no children.)
Example 1:

Input: [0,1,2,3,4,3,4]
Output: "dba"
Example 2:

Input: [25,1,3,1,3,0,2]
Output: "adz"
Example 3:

Input: [2,2,1,null,1,0,null,0]
Output: "abc"
Note:
- The number of nodes in the given tree will be between
1and8500. - Each node in the tree will have a value between
0and25.
题解:
Do dfs and get all possible results. Maintain the smallest one.
Update the res only at leaf node, but not when node is null. Otherwise, it would get wrong result.
e.g. [1,2], if update res at null, when iterating right child, since it is null, current string is "a", smaller than existing "ba", it would update res. But actually it is not string starting from leaf.
For questions specifically mentioned leaf, should update node only at leaf node.
Time Complexity: O(n).
Space: O(h).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
String res = "~"; public String smallestFromLeaf(TreeNode root) {
if(root == null){
return res;
} dfs(root, new StringBuilder());
return res;
} private void dfs(TreeNode root, StringBuilder sb){
if(root == null){
return;
} sb.insert(0, (char)('a'+root.val)); // Update res only at leaf node
if(root.left == null && root.right == null){
if(sb.toString().compareTo(res) < 0){
res = sb.toString();
}
} dfs(root.left, sb);
dfs(root.right, sb); sb.deleteCharAt(0);
}
}
类似Sum Root to Leaf Numbers, Binary Tree Paths.
LeetCode 988. Smallest String Starting From Leaf的更多相关文章
- LC 988. Smallest String Starting From Leaf
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【leetcode】988. Smallest String Starting From Leaf
题目如下: Given the root of a binary tree, each node has a value from 0 to 25representing the letters 'a ...
- [Swift]LeetCode988. 从叶结点开始的最小字符串 | Smallest String Starting From Leaf
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to ...
- 【leetcode】1202. Smallest String With Swaps
题目如下: You are given a string s, and an array of pairs of indices in the string pairs where pairs[i] ...
- C. The Smallest String Concatenation
C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...
- codeforces 632C The Smallest String Concatenation
The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...
- CodeForces 632C The Smallest String Concatenation//用string和sort就好了&&string的基础用法
Description You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them togethe ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
随机推荐
- Hive 系列(三)—— Hive CLI 和 Beeline 命令行的基本使用
一.Hive CLI 1.1 Help 使用 hive -H 或者 hive --help 命令可以查看所有命令的帮助,显示如下: usage: hive -d,--define <key=va ...
- 在.net core中数据操作的两种方式(Db first && Code first)
在开发过程中我们通常使用的是Db first这种模式,而在.net core 中推荐使用的却是 code first 反正我是很不习惯这种开发模式 于是就搜寻整个微软的官方文档,终于找到了有关.net ...
- Java自学-操作符 位操作符
Java的位操作符 位操作符 在实际工作中使用并不常见. 示例 1 : 一个整数的二进制表达 位操作都是对二进制而言的,但是我们平常使用的都是十进制比如5. 而5的二进制是101. 所以在开始学习之前 ...
- sql分页优化
索引优化 注意查询的数据占总数据达到一定量的时候可能导致索引失效.可以用limit或者指定列缩小数据区域可以解决. 以时间orderby排序的limit分页优化 前提用order by分页 limit ...
- (转) Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1
(转)python(三):Python3-UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1 python ...
- python day 21: HTML的基本元素及CSS
目录 python day 21 1. HTML 1.1 常见的HTML元素 python day 21 2019/11/02 学习资料来自老男孩与尚学堂 1. HTML 1.1 常见的HTML元素 ...
- electron-vue多显示屏下将新窗口投放是其他屏幕
display对象可以获取所有显示屏此处演示程序启动是投放新窗口至另一屏幕 import { app, BrowserWindow } from 'electron' const electron = ...
- 英语wacche腕表
手表 (戴在手腕上的计时仪器) 手表,或称为腕表,是指戴在手腕上,用以计时/显示时间的仪器,手表在英语里watch源自中世纪wacche这一词汇. 手表通常是利用皮革.橡胶.尼龙布.不锈钢等材料,制成 ...
- 【DATAGUARD】物理dg配置客户端无缝切换 (八.1)--Data Guard Broker 的配置
[DATAGUARD]物理dg配置客户端无缝切换 (八.1)--Data Guard Broker 的配置 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技 ...
- mysql数据库创建、查看、修改、删除
一.创建数据库 使用默认字符集 不指定字符集时,mysql使用默字符集,从mysql8.0开始,默认字符集改为utf8mb4 ,创建数据库的命令为create database 数据库名称. #创建数 ...