当前位置:网站首页>[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 ~
边栏推荐
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
- New product experience: Alibaba cloud's new generation of local SSD instance I4 open beta
- Double buffer drawing
- How about stock online account opening and account opening process? Is it safe to open a mobile account?
- 猫狗队列
- Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
- thinkphp6 多级控制器目录访问解决方法
- Pytorch model parameter adjustment and training related contents
- Connected to rainwater series problems
- MySQL从库Error:“You cannot ‘Alter‘ a log table...“
猜你喜欢

(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)

From PDB source code to frame frame object

PostgreSQL surpasses MySQL

2.01 backpack problem

Websocket automatically disconnects in 1 minute

Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event

药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计

中国广电5G套餐来了,比三大运营商低,却没预期那么低

China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected

DevEco Studio 3.0编辑器配置技巧篇
随机推荐
抢做意大利岛主?刘强东两月套现66亿 疑一次性5.6亿“紧急转账”急购欧洲海上皇宫
PCB懂王,你是吗?我不是
增额终身寿险有哪些产品可以买呢?
Visual design tutorial of word cloud
thinkphp6 多级控制器目录访问解决方法
Double buffer drawing
First knowledge of exception
Inftnews | technology giants accelerate their march into Web3 and metauniverse
Kubernetes in-depth understanding of kubernetes (I)
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
嵌入式设计与开发项目-液位检测告警系统
Analysis and processing of GPS data format [easy to understand]
真香啊!最全的 Pycharm 常用快捷键大全!
Stackoverflow 2022 database annual survey
Recognize the startup function and find the user entry
iNFTnews | 科技巨头加快进军Web3和元宇宙
PHP crawls web pages for specific information
Notes on the use of official jeecg components (under update...)
求解汉诺塔问题
[机缘参悟-32]:鬼谷子-抵巇[xī]篇-面对危险与问题的五种态度