原题链接在这里: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:

  1. The number of nodes in the given tree will be between 1 and 8500.
  2. Each node in the tree will have a value between 0 and 25.

题解:

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 NumbersBinary Tree Paths.

LeetCode 988. Smallest String Starting From Leaf的更多相关文章

  1. 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  ...

  2. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  3. 【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 ...

  4. [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  ...

  5. 【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] ...

  6. C. The Smallest String Concatenation

    C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...

  7. codeforces 632C The Smallest String Concatenation

    The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...

  8. 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 ...

  9. Educational Codeforces Round 9 C. The Smallest String Concatenation 排序

    C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...

随机推荐

  1. Python中使用@的理解

    Python函数中使用@ 稍提一下的基础 fun 和fun()的区别 以一段代码为例: def fun(): print('fun') return None a = fun() #fun函数并将返回 ...

  2. Fiddler的使用总结

    关于Fiddler的使用过程中的总结: 1. 配置手机抓包的过程,以后再补充 2.使用Fiddler发送请求 1) 第一步 抓取接口,获取请求方式,以及请求参数  2) 第二步 请求接口 点击Exec ...

  3. MOOC 数据库笔记(四):关系代数

    关系代数 关系代数概述 特点 基于集合,提供了一系列的关系代数操作:并.差.笛卡尔积(广义积).选择.投影和更名等基本操作 以及交.连接和关系除等扩展操作,是一种集合思维的操作语言. 关系代数操作以一 ...

  4. linux 不能进入系统 Failed to load SELinux policy. Freezing

    错误原因 配置关闭SELinux,结果误操作 应修改配置文件/etc/selinux/config中的“SELINUX”参数的值, # SELINUX=enforcing 原始配置 SELINUX=d ...

  5. 让js中的函数只有一次有效调用

    设置隐藏域~ <input type="hidden" value="1" id="flag" /> 其它三种方法

  6. python全栈学习路线

    python全栈学习路线-查询笔记 查询目录 一,硬件                                                                    十一,数据 ...

  7. MyCat - 数据库中间插件

    什么是MyCat 是目前最流行的分布式数据库中间插件 为什么使用MyCat 如今随着互联网的发展,数据的量级也是撑指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经 ...

  8. 仿百度图片首页--HTML+CSS练手项目1【Table】

    [本文为原创,转载请注明出处] 技术[CSS+HTML]   布局[Table] 图片准备[百度图标.10张不同类型图] --------------------------------------- ...

  9. 在eclipse下给android应用添加jar包

    右键工程,Build path,java build path,选择libraries在右边的按钮中点击“Add Library”选择“User library”,点击“下一步”点击“User lib ...

  10. [LeetCode] 647. 回文子串 ☆☆☆(最长子串、动态规划、中心扩展算法)

    描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串. 示例 1: 输入: "abc" ...