当前位置:网站首页>[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 ~
边栏推荐
- Bridge emqx cloud data to AWS IOT through the public network
- Tencent cloud installs MySQL database
- Nielseniq welcomes dawn E. Norvell, head of retail lab, to accelerate the expansion of global retail strategy
- 分享 5 大常用的特征选择方法,机器学习入门必看!!!
- svg实现的订票UI效果
- 新技能:通过代码缓存加速 Node.js 的启动
- Spin lock exploration
- Login box tricks
- 理解现货白银走势的关键
- 数据中心的能耗焦虑, 到底有没有最优解?
猜你喜欢

Horizontal visual error effect JS special effect code

IEEE TBD SCI影响因子提升至4.271,位列Q1区!

K-line diagram interpretation and practical application skills (see position entry)

The new version of Shangding cloud | favorites function has been launched to meet personal use needs

JS from prototype chain to inheritance

Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution

Spin lock exploration

Importing alicloud ECS locally to solve deployment problems

Splitting. JS text title slow loading JS effect

5G商用三年,未来创新何去何从?
随机推荐
每日面试1题-蓝队基础面试题-应急响应(1)应急响应基本思路流程+Windows入侵排查思路
Generate confrontation network, from dcgan to stylegan, pixel2pixel, face generation and image translation.
Rexroth hydraulic control check valve z2s10-1-3x/
Development: how to install offline MySQL in Linux system?
Map collection
Nielseniq welcomes dawn E. Norvell, head of retail lab, to accelerate the expansion of global retail strategy
Hyper-v:在虚拟网络中启用 SR-IOV
联想“双平台”运维解决方案 助力智慧医疗行业智慧管理能力全面提升
MySQL之零碎知识点
【Proteus仿真】Arduino UNO利用74LS148扩展中断
Design and principle of tubes responsive data system
Plane intersection and plane equation
Exch:exchange server 2013 is about to end support
New skill: accelerate node through code cache JS startup
开发那些事儿:Linux系统中如何安装离线版本MySQL?
DeFi借贷协议机制对比:Euler、Compound、Aave和Rari Capital
【剑指Offer】52. 两个链表的第一个公共节点
[sword finger offer] sword finger offer 53 - ii Missing numbers from 0 to n-1
New power of data analysis -- the first open source integrated real-time HTAP database in China was released by stonedb
构建基本buildroot文件系统