当前位置:网站首页>[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 ~
边栏推荐
- Prediction of red wine quality by decision tree
- Luogu_ P1303 A*B Problem_ High precision calculation
- Kubernetes 深入理解kubernetes(一)
- 初识exception
- 锐捷交换机配置ssh password登录命令[通俗易懂]
- China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
- Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系
- 排序
- 接雨水系列问题
- Kubernetes 深入理解Kubernetes(二) 声明组织对象
猜你喜欢
PCB understand Wang, are you? I am not
From PDB source code to frame frame object
2022年中国运维安全产品市场规模及发展趋势预测分析
真香啊!最全的 Pycharm 常用快捷键大全!
Forecast and Analysis on market scale and development trend of China's operation and maintenance security products in 2022
药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
程序员坐牢了,会被安排去写代码吗?
Inftnews | technology giants accelerate their march into Web3 and metauniverse
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
开源社邀您参加OpenInfra Days China 2022,议题征集进行中~
随机推荐
单元测试 CI/CD
G : 最大流问题
ThreadLocal的简单理解
众昂矿业着眼氟化工产业,布局新能源产业链
MySQL slave error: "you cannot 'alter' a log table“
How fragrant! The most complete list of common shortcut keys for pychar!
Native JS implements drag and drop of page elements
PostgreSQL超越MySQL
Class structure in C language - dot
Resume template Baidu online disk
Regular matching numbers, English and English symbols
BERT为何无法彻底干掉BM25??
Stackoverflow 2022 database annual survey
Is it safe for Huatai Securities to open an account? Is there any risk in opening an account
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
木兰开放作品许可证1.0面向社会公开征求意见
Websocket automatically disconnects in 1 minute
PostgreSQL surpasses MySQL
yii2编写swoole的websocket服务
Jeecg 官方组件的使用笔记(更新中...)