当前位置:网站首页>[binary tree] the minimum string starting from the leaf node
[binary tree] the minimum string starting from the leaf node
2022-06-28 13:49:00 【It's so cold】
0x00 subject
Given a root node as root Two fork tree
Every node in the tree has a [0, 25] Values in range
Respectively representing letters a To z
return Smallest in lexicographic order String
The string from one of the trees leaf Start
To end of root
notes :
Any shorter prefix in the string is in Dictionary Preface All are smaller Of
for example , In dictionary order “ab” Than “aba” smaller
Leaf node refers to node without child node
0x01 Ideas
Because the composition of the string is
from Leaf node towards The root node The direction of
So use In the following order Traverse the way
0x02 solution
Language :Swift
Tree node :TreeNode
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
self.right = right
}
}
solution :
func smallestFromLeaf(_ root: TreeNode?) -> String {
var res = ""
var s = ""
func dfs(_ root: TreeNode?) {
guard let r = root else { return }
// Number to character ,a - 97
let c = Character(UnicodeScalar(97 + r.val)!)
// Character to string , Splicing
s += String(c)
// At the leaf node
if r.left == nil && r.right == nil {
var t = String(s)
// In reverse order
t = String(t.reversed())
// Compare the size
if res.isEmpty || t < res {
res = t
}
}
dfs(r.left)
dfs(r.right)
// Go back to the previous level , Delete the last one , Backtracking algorithm
s.removeLast()
}
dfs(root)
return res
}
0x03 My work
Welcome to experience one of my works : Small five pen small program
Wubi learning is a good helper ~
WeChat search XWubi that will do ~
边栏推荐
- [understanding of opportunity -32]: Guiguzi - Dui [x ī] Five attitudes towards danger and problems
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
- Native JS implements drag and drop of page elements
- Luogu_ P1303 A*B Problem_ High precision calculation
- SPI接口简介-Piyu Dhaker
- yii2编写swoole的websocket服务
- 真香啊!最全的 Pycharm 常用快捷键大全!
- 设计一个有getMin功能的栈
- 锐捷交换机配置ssh password登录命令[通俗易懂]
- 2022年中国运维安全产品市场规模及发展趋势预测分析
猜你喜欢
随机推荐
Simple understanding of ThreadLocal
ThreadLocal的简单理解
Yii2 writing the websocket service of swoole
How to set auto format after saving code in vscade
[understanding of opportunity -32]: Guiguzi - Dui [x ī] Five attitudes towards danger and problems
Kubernetes 深入理解kubernetes(一)
Hundreds of lines of code to implement a JSON parser
How to design data visualization platform
Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event
欧拉恒等式:数学史上的真正完美公式!
CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
En parlant d'exception - que se passe - t - il lorsque l'exception est lancée?
PHP crawls web pages for specific information
NFT digital collection system development (3D modeling economic model development case)
Votre Code parle? (1)
嵌入式设计与开发项目-液位检测告警系统
再談exception——异常拋出時會發生什麼?
药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects








