题目如下:

Given the root of a binary tree, each node has a value from 0 to 25representing 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 1000.
  2. Each node in the tree will have a value between 0 and 25.

解题思路:把树遍历一下就好了,依次记录从根节点开始每一层的节点的值,到达叶子节点后比较得到最小值。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
res = 'z'*1001
def recursive(self,node,path):
path += chr(node.val + ord('a'))
if node.left == None and node.right == None:
self.res = min(self.res,path[::-1])
if node.left != None:
self.recursive(node.left,path)
if node.right != None:
self.recursive(node.right, path) def smallestFromLeaf(self, root):
"""
:type root: TreeNode
:rtype: str
"""
if root != None:
self.recursive(root,'')
return self.res

【leetcode】988. Smallest String Starting From Leaf的更多相关文章

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

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

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

  3. LeetCode 988. Smallest String Starting From Leaf

    原题链接在这里:https://leetcode.com/problems/smallest-string-starting-from-leaf/ 题目: Given the root of a bi ...

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

  5. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  6. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  7. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

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

  9. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

随机推荐

  1. 【leetcode】553. Optimal Division

    题目如下: 解题思路:这是数学上的一个定理.对于x1/x2/x3/..../xN的序列,加括号可以得到的最大值是x1/(x2/x3/..../xN). 代码如下: class Solution(obj ...

  2. chromedriver与chrome版本映射表(更新至v2.46)

    chromedriver版本 支持的Chrome版本 v2.46 v71-73 v2.45 v70-72 v2.44 v69-71 v2.43 v69-71 v2.42 v68-70 v2.41 v6 ...

  3. LOJ 6433 「PKUSC2018」最大前缀和——状压DP

    题目:https://loj.ac/problem/6433 想到一个方案中没有被选的后缀满足 “该后缀的任一前缀和 <=0 ”. 于是令 dp[ S ] 表示选了点集 S ,满足任一前缀和 & ...

  4. TP中如何用IF

    将TP中这个容易忘的知识点记下来以便日后翻阅 $memberField = "ID, NAME, MOBILE, MEMBER_STATUS as status, IF (MEMBER_ST ...

  5. Bootstrap Date Range Picker

    var optionSet1 = { startDate: moment().subtract(29, 'days'), endDate: moment(), minDate: '12/21/2012 ...

  6. java中的fail-fast(快速失败)机制

    java中的fail-fast(快速失败)机制 简介 fail-fast机制,即快速失败机制,是java集合中的一种错误检测机制.当在迭代集合的过程中对该集合的结构改变是,就有可能会发生fail-fa ...

  7. rafy使用笔记

    1.rafy里实体字段string类型映射成数据库varchar(2000). 解决:“DomainApp“下“OnRuntimeStarting“方法里添加“DbMigrationSettings. ...

  8. php Connection timed out after 30000 milliseconds

    function HttpRequest($url, $params, $method = 'GET', $header = array(), $bEncode = true){ $opts = ar ...

  9. 在Emacs中使用plantuml画UML图

    在Emacs中使用plantuml画UML图 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #83 ...

  10. pixi与lottie-web的benchmark测试

    生产版本 "dependencies": {     "lottie-web": "^5.5.7",     "pixi-spin ...