当前位置:网站首页>[binary tree] maximum binary tree II
[binary tree] maximum binary tree II
2022-06-30 01:29:00 【It's so cold】
0x00 subject
The biggest tree Definition :
A tree , among Every The value of the node
all Greater than Any other value in its subtree
Give you the root node of the largest tree root And an integer val
hold val Insert this tree
And return the new largest binary tree root node
0x01 Ideas
According to the The rules root The nodes are in a given array Maximum value
Maximum On the left yes Left subtree
Maximum On the right yes Right subtree
Newly inserted val Is added to a given array Last Of
therefore val Should be inserted into Right 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 insertIntoMaxTree(_ root: TreeNode?, _ val: Int) -> TreeNode? {
// Node is empty , Then a new node is returned
guard let r = root else { return TreeNode(val) }
// Node value is smaller than insert value , Find the location
if r.val < val {
let node = TreeNode(val)
node.left = r
return node
}
// Insert into the right subtree
r.right = insertIntoMaxTree(r.right, val)
return r
}
0x03 My work
Welcome to experience one of my works : Small editor
Small online editor
contain Varied Language App Store Search ~
边栏推荐
- 【PyTorch实战】生成对抗网络GAN:生成动漫人物头像
- R语言线性回归模型拟合诊断异常值分析家庭燃气消耗量和卡路里实例带自测题
- Cookie encryption 12
- Ctfshow competition original title 680-695
- C语言 数素数
- post请求出现WebKitFormBoundaryk的解决办法
- Vl6180x distance and light sensor hands-on experience
- How to deal with occasional bugs?
- I, 33 years old, ByteDance test development, unveiled the real income of Beijing "test post"
- C语言 害死人不偿命的(3n+1)猜想
猜你喜欢

C语言 数组元素循环右移问题

js返回内容被unicode编码

Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you

【PyTorch实战】生成对抗网络GAN:生成动漫人物头像
![Cantilever beam calculation [matlab code]](/img/f6/9a1338e00777e91f6c340eae9d52ba.jpg)
Cantilever beam calculation [matlab code]

Cookie encryption 8

C语言 素数对猜想

Mechanical --nx2007 (UG) -- gap analysis (interference inspection)

C语言 继续(3n+1)猜想

The first technology podcast month will be launched soon
随机推荐
【图神经网络】图分类学习研究综述[3]:图分类方法评价及未来研究方向
C语言 说反话
模块导入reload方法
MES管理系统功能模块之质量管理
Mysql 监控
Difference between test plan and test plan
Varnish 基础概览10
Statsmodels notes STL
Precautions for postoperative fundus hemorrhage / / must see every day
Varnish 基础概览4
Varnish 基础概览8
ES6 synchronous asynchronous execution and block level scope
Pytroch Learning Notes 6: NN network layer convolution layer
JS prototype and prototype chain (Lantern Festival meal)
关于c语言main函数中int argc,char **argv的理解
R language linear regression model fitting diagnosis outliers analysis of domestic gas consumption and calorie examples with self-test questions
Varnish 基础概览2
What is digital garbage? Follow the world's first AI artist to explore meta carbon Art
Sentinel source code analysis Part 6 - sentinel adapter module Chapter 4 zuul2 gateway
Some thoughts on small program subcontracting and verification of uiniapp subcontracting optimization logic