【leetcode】988. Smallest String Starting From Leaf
题目如下:
Given the
rootof a binary tree, each node has a value from0to25representing the letters'a'to'z': a value of0represents'a', a value of1represents'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
1and1000.- Each node in the tree will have a value between
0and25.
解题思路:把树遍历一下就好了,依次记录从根节点开始每一层的节点的值,到达叶子节点后比较得到最小值。
代码如下:
# 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 ...
随机推荐
- html中 的method=post和method=get的区别
1. get是从服务器上获取数据,post是向服务器传送数据. 2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过 ...
- 【leetcode】1023. Camelcase Matching
题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...
- Setting proxy for Windows CMD and Linux Terminal
setting proxy for Windows CMD and Linux Terminal Linux Terminal: set http_proxy=http://127.0.0.1:811 ...
- BZOJ 4883 棋盘上的守卫 解题报告
BZOJ4883 棋盘上的守卫 考虑费用流,但是数据范围太大 考虑 \(i\) 行 \(j\) 列如果被选择,那么要么给 \(i\) 行,要么给 \(j\) 列 把选择 \(i\) 行 \(j\) 列 ...
- 【Linux】批量结束某一脚本的进程
ps -ef | grep **.sh |grep -v grep | awk '{print $2}' | xargs kill -9
- mysql审计插件
Audit Plugin安装使用 原文: https://www.cnblogs.com/waynechou/p/mysql_audit.html#_label0 #有卸载方法 下载地址: htt ...
- mysql隔离级别相关
1.原子性.隔离性.一致性.持久性 2.mysql并发控制可能出现的问题: 脏读(A事务读取到B事务未commit的数据后,B事务回滚) 不可重复读(A事务第一次读到的数据,被B事务更新数据后,第二次 ...
- Leaflet-Leafletv0.7使用leaflet-bing-layer
digidem/leaflet-bing-layer: Bing Maps Layer for Leaflet v1.0.0:从标题就可以看出要Leaflet v1.0.0才能用.其实leaflet ...
- hive sql基础了解
会有些不一样 1 例如使用SQL 之前,要了解用了那个库,use jz_daojia 2 使用GET_JSON_OBJECT 函数等,以及参数 匹配 $.childBrithDay 挺有意思的.新玩意 ...
- Php邮件发送源码
好久冒写点东西了.....最近生活压抑的很....为生活而劳累,整理下邮件发送的实例了,网上也有很多,我这个也是提取整理好的,测试Ok,首页邮件类smtp_email_class.php如下:< ...


