当前位置:网站首页>【二叉树】前序遍历构造二叉搜索树
【二叉树】前序遍历构造二叉搜索树
2022-06-30 16:36:00 【豪冷啊】
0x00 题目
给定一个整数数组
它表示BST(即二叉搜索树 )的 先序遍历
构造树并返回其根节点
0x01 思路
二叉搜索树的前序遍历
第一个元素是根节点
往后的元素小于根节点的是左子树的元素大于根节点的是右子树的元素
0x02 解法
语言:Swift
树节点: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
}
}
解法:
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]
// 在区间 [l...r] 内找 最后一个 小于 first 的下标
while l < r {
let mid = l + (r - l + 1) / 2
// 小于往右找
if preorder[mid] < first {
l = mid
}else{
r = mid - 1
}
}
// 第一个元素是根节点
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 我的作品
欢迎体验我的作品之一:小笔记-XNote
让笔记一步到位App Store 搜索即可~
边栏推荐
- Nouvelle version de shangdingyun | la fonction favorite est en ligne pour répondre aux besoins d'utilisation personnelle
- MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
- MIT科技评论2022年35岁以下创新者名单发布,含AlphaFold作者等
- EMQ helps Qingdao Yanbo build a smart water platform
- Development details of NFT casting trading platform
- [sword finger offer] sword finger offer 53 - ii Missing numbers from 0 to n-1
- Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)
- Parker variable displacement piston pump pv092r1k1t1nmmc
- MySQL之零碎知识点
- Network: principle and practice of server network card group technology
猜你喜欢

China Infrastructure Development Association: electronic contract is recommended

New skill: accelerate node through code cache JS startup

巴比特 | 元宇宙每日必读:未成年人打赏后要求退款,虚拟主播称自己是大冤种,怎么看待这个监管漏洞?...

Six pictures show you why TCP has three handshakes?

5G商用三年,未来创新何去何从?

Generate confrontation network, from dcgan to stylegan, pixel2pixel, face generation and image translation.

Login box tricks

Compile and generate busybox file system

k线图精解与实战应用技巧(见位进场)

Rexroth hydraulic control check valve z2s10-1-3x/
随机推荐
Spin lock exploration
New research of HKUST & MsrA: about image to image conversion, finishing is all you need
高等数学(第七版)同济大学 总习题一 个人解答
Share 5 commonly used feature selection methods, and you must see them when you get started with machine learning!!!
K-line diagram must be read for quick start
【C语言】详解线程 — 线程分离函数 pthread_detach
每日面试1题-如何防止CDN防护被绕过
. Net ORM framework hisql practice - Chapter 1 - integrating hisql
IEEE TBD SCI impact factor increased to 4.271, ranking Q1!
Exploration and practice of "flow batch integration" in JD
Interview shock 60: what will cause MySQL index invalidation?
【剑指Offer】剑指 Offer 53 - II. 0~n-1中缺失的数字
Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
Vue3 reactive database
【C语言】详解线程 — 开启两个线程
How can you choose to work in the county after graduation?
【Proteus仿真】Arduino UNO利用74LS148扩展中断
Nouvelle version de shangdingyun | la fonction favorite est en ligne pour répondre aux besoins d'utilisation personnelle
China Infrastructure Development Association: electronic contract is recommended
New skill: accelerate node through code cache JS startup