当前位置:网站首页>[binary tree] preorder traversal to construct binary search tree
[binary tree] preorder traversal to construct binary search tree
2022-06-30 17:56:00 【It's so cold】
0x00 subject
Given a Integers Array
It said BST( The binary search tree ) Of The first sequence traversal
Construct the tree and return its The root node
0x01 Ideas
Of binary search trees Before the order Traverse
The first element is root node
Subsequent elements Small At the root node is Left Elements of the subtree Big At the root node is Right Elements of the subtree
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 bstFromPreorder(_ preorder: [Int]) -> TreeNode? {
if preorder.isEmpty { return nil }
func build(_ start: Int, _ end: Int) -> TreeNode? {
if start > end { return nil }
else if start == end { return TreeNode(preorder[start]) }
var l = start
var r = end
let first = preorder[start]
// In the interval [l...r] Internal search the last one Less than first The subscript
while l < r {
let mid = l + (r - l + 1) / 2
// Look right
if preorder[mid] < first {
l = mid
}else{
r = mid - 1
}
}
// The first element is the root node
let node = TreeNode(first)
node.left = build(start+1, l)
node.right = build(l+1, end)
return node
}
let r = build(0, preorder.count-1)
return r
}
0x03 My work
Welcome to experience one of my works : Little notes -XNote
Take notes one step at a time App Store Search ~
边栏推荐
- Exch: database integrity checking
- Inventory in the first half of 2022: summary of major updates and technical points of 20+ mainstream databases
- Development details of NFT casting trading platform
- Splitting. JS text title slow loading JS effect
- [zero basic IOT pwn] environment construction
- EMQ helps Qingdao Yanbo build a smart water platform
- splitting.js密码显示隐藏js特效
- 【义修换届大礼包】
- Tencent cloud installs MySQL database
- [C language] detailed explanation of threads - multi threads for collaborative operation
猜你喜欢

Cloud practice of key business migration of Internet of things by well-known Internet housing rental service companies

Course design for the end of the semester: product sales management system based on SSM

生成对抗网络,从DCGAN到StyleGAN、pixel2pixel,人脸生成和图像翻译。

Acwing game 57

Booking UI effect implemented by svg

How can you choose to work in the county after graduation?

Splitting.js文本标题缓慢加载js特效
![万卷书 - 书单整理 [01]](/img/d4/124101b919a4d8163a32fc0f158efa.png)
万卷书 - 书单整理 [01]

Word中添加代码块(转载)

零基础也能做Apple大片!这款免费工具帮你渲染、做特效、丝滑展示
随机推荐
TFTP download kernel, NFS mount file system
后渗透之文件系统+上传下载文件
知名互联网房屋租赁服务公司物联网关键业务迁移上云实践
[零基础学IoT Pwn] 环境搭建
Analysis on the construction scheme and necessity of constructing expressway video monitoring platform
JS from prototype chain to inheritance
Parker Parker sensor p8s-grflx
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
编写C语言的最简单小程序Hello world
MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
Development details of NFT casting trading platform
【剑指Offer】53 - I. 在排序数组中查找数字 I
Importing alicloud ECS locally to solve deployment problems
Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
Send the injured baby for emergency medical treatment. Didi's driver ran five red lights in a row
Vue3 reactive database
Design and principle of tubes responsive data system
Nielseniq welcomes dawn E. Norvell, head of retail lab, to accelerate the expansion of global retail strategy
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution