当前位置:网站首页>[binary tree] balance the binary search tree
[binary tree] balance the binary search tree
2022-07-26 19:53:00 【How cold it is】
0x00 subject
Here's a binary search tree , Please return to a tree After balancing Binary search tree of
The newly generated tree should be different from the original tree same Node values
If there are multiple construction methods , Please return Any one
If a binary search tree
Two subtrees of each node Height difference No more than 1
We call this binary search tree The balance of
0x01 Ideas
Through the binary search tree In the sequence traversal
Get one Ascending array
Then build a balanced binary tree through an ascending array
Take an array middle Number of numbers , Generate root node
Then the number of left and right sides of the array Difference value Not more than 1 recursive Just build the left and right parts
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 balanceBST(_ root: TreeNode?) -> TreeNode? {
var arr: [Int] = []
// In the sequence traversal
func dfs(_ root: TreeNode?) {
guard let r = root else { return }
dfs(r.left)
arr.append(r.val)
dfs(r.right)
}
// Build a balanced binary tree
func build(_ nums: [Int], _ left: Int, _ right: Int) -> TreeNode? {
if left > right { return nil }
// Take the median
let mid = (left + right) / 2
let val = nums[mid]
let node = TreeNode(val)
// Recursively build
node.left = build(nums, left, mid-1)
node.right = build(nums, mid+1, right)
return node
}
dfs(root)
let node = build(arr, 0, arr.count-1)
return node
}
0x03 My little work
Welcome to experience one of my works : Little notes -XNote
Make notes , One step in place !
Pass notes , One click !App Store Search ~
边栏推荐
- Redis6
- Collection of original IOS interview questions
- MySQL tutorial: MySQL database learning classic (from getting started to mastering)
- 中信建投启牛学堂开户是安全的吗,启牛是干嘛的
- openstack 虚拟机网卡被重名为cirename0
- jar文件 反编译(IDEA环境)
- Cuda11.2 corresponding pytorch installation
- [internship experience] exception handling and URL result response data processing
- Pyqt5 rapid development and practice 3.6 packaging resource files
- YOLO V1详解
猜你喜欢

DOM案例:10秒倒计时-写跳转页面相关的知识
![[internship experience] exception handling and URL result response data processing](/img/ed/05622fad0d3d8dcf17ce7069340669.jpg)
[internship experience] exception handling and URL result response data processing
Bug 反馈:同步失败

Still using xshell? You are out. I recommend a more modern terminal connection tool

Linear algebra Chapter 3 vector

How to compress the traffic consumption of APP under mobile network in IM development

企业数字化转型成大趋势,选对在线协作工具很重要

canvas概述

Leetcode daily practice - 88. Merge two ordered arrays

u盘损坏怎么恢复原来数据,u盘损坏数据如何恢复
随机推荐
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
Leetcode daily practice - 189. Rotation array
中天钢铁在 GPS、 AIS 调度中使用 TDengine
浅析接口测试
Leetcode daily practice - 26. Delete duplicates in an ordered array
基于ABP实现DDD--领域逻辑和应用逻辑
基于华为云 IOT 设计智能称重系统 (STM32)【一】
调整数组顺序使奇数位于偶数前面且相对位置不变
【PHP】将 SESSION 数据保存到 Redis
工作13年后,个人的一点软件测试经历及感想……
Digital transformation of enterprises has become a general trend, and it is important to choose the right online collaboration tools
中信建投启牛学堂开户是安全的吗,启牛是干嘛的
Solidity中call函数详解
C# .net 时间戳和时间转换 支持时区
Analysis of interface testing
Pads draw 2.54mm row needle
Redis6
Reentrantlock learning - Basic Attributes
Will 3R balanced financial products have risks? Is it risky?
聊聊如何用 Redis 实现分布式锁?