【leetcode】988. Smallest String Starting From Leaf
题目如下:
Given the
root
of a binary tree, each node has a value from0
to25
representing the letters'a'
to'z'
: a value of0
represents'a'
, a value of1
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
1
and1000
.- Each node in the tree will have a value between
0
and25
.
解题思路:把树遍历一下就好了,依次记录从根节点开始每一层的节点的值,到达叶子节点后比较得到最小值。
代码如下:
# 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的更多相关文章
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 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
原题链接在这里:https://leetcode.com/problems/smallest-string-starting-from-leaf/ 题目: Given the root of a bi ...
- 【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] ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- [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】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
随机推荐
- Factory Methods
The static factory method pattern is a way to encapsulate object creation. Without a factory method, ...
- Java反射学习-5 - 反射复制对象
通过反射方式复制对象: package cn.tx.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Fi ...
- Code Review怎样做好
一.背景 最近随着交易业务快速扩展,研发组内新项目及新成员越来越多,如何做好Code Review,把控研发人员开发代码质量很是关键. 对于大部分业务团队,谈到Code Review就会面露哀状: ...
- [CSP-S模拟测试73]题解
A.小P的2048 作为一个看B哥玩了一个寒假的人这种题闭眼切好吧 模拟即可.程序模块化后直接复制粘贴. 说什么模拟不能复制粘贴的都没水平 #include<cstdio> #includ ...
- CodeForces 593D Happy Tree Party
题目链接: http://codeforces.com/problemset/problem/593/D ----------------------------------------------- ...
- Design:设计(活动)百科
ylbtech-Design:设计(活动)百科 设计是把一种设想通过合理的规划.周密的计划.通过各种感觉形式传达出来的过程.人类通过劳动改造世界,创造文明,创造物质财富和精神财富,而最基础.最主要的创 ...
- HTML最全标签
一.HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(DTD). 标签:a 说明:标明超链接的起始或目的位置. 标签:acronym 说明:标明缩写词. ...
- 测开之路三十五:css引入
CSS是一种定义样式结构,如字体.颜色.位置等的语言,被用于描述网页上的信息格式化和现实的方式.CSS样式可以直接存储于HTML网页或者单独的样式单文件.无论哪一种方式,样式单包含将样式应用到指定类型 ...
- latex的资料ftp
ftp://ftp.tex.ac.uk/ctan/tex-archive/ ftp ftp.tex.ac.uk anonymous ls
- l1和l2正则化
https://blog.csdn.net/tianguiyuyu/article/details/80438630 以上是莫烦对L1和L2的理解 l2正则:权重的平方和,也就是一个圆 l1正则:权重 ...