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 ...
随机推荐
- 小菜鸡deepin系统手动更新火狐浏览器
前言 Deepin 是个好系统,让我看到国产系统的希望,也让我看到Linux桌面化和大众化的可能(如果你想抬杠:Deepin只是Linux魔改没什么好显摆的.那--你开心就好 ^ _ ^ ).虽然有一 ...
- The One day 中位数的计算
""" 中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / ...
- React+SpringBoot项目部署
静态资源访问配置 https://www.jianshu.com/p/b6e0a0df32ec https://segmentfault.com/q/1010000012240531/a-102000 ...
- SQL Server——死锁查看
一.通过语句查看 --查询哪些死锁SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableN ...
- springboot IDEA新建Maven项目的Plugins出现红线的解决方法
将pom.xml文件copy到桌面,删除项目中的pom.xml.发现项目maven中没有任何东西后,然后将桌面的pom.xml粘贴到项目目录下,刷新maven就ok了
- dump net core lldb 分析
原文https://www.cnblogs.com/calvinK/p/9274239.html centos7 lldb 调试netcore应用的内存泄漏和死循环示例(dump文件调试) 写个dem ...
- Github下载慢和下载过程中断等情况的解决方案
Github下载慢和下载过程中断等情况的解决方案 最近老大push项目,正常的git clone每次都是下载一部分就断掉了. 尝试了修改hosts文件的方式,更换了延迟最低的域名也没啥用(难道我姿 ...
- 两种方法实现在HTML页面加载完毕后运行JS
JS默认方法: <script type=”text/javascript”> window.onload=function (){ /*代码区域*/ } </script> ...
- 深入理解es6(下)
一.symbol javascript基本数据类型: null.undefined.number.boolean.string.symbol ES6 引入了一种新的原始数据类型Symbol,表示独一无 ...
- 《区块链DAPP开发入门、代码实现、场景应用》笔记2——Solidity实现简单的智能合约
本节仅以一个简单的智能合约示例,介绍智能合约的基本组成元素,本合约定义一个uint类型的变量,以及对应这个变量的读写函数. 01 pragma solidity >=0.4.0 <0.6. ...