当前位置:网站首页>[binary tree] maximum product of split binary tree
[binary tree] maximum product of split binary tree
2022-07-21 15:10:00 【How cold it is】
0x00 subject
Here is a binary tree for you , Its root is root
Please delete 1
side , Split the binary tree into two subtree
And their subtrees and
Of The product of
As big as possible
Because the answer could be big
Please put the result to 10^9 + 7
Take the mold and return to
0x01 Ideas
Because the tree should be divided into 2
part
Suppose the sum of the first tree after segmentation is S
Then the sum of the remaining parts is the sum of the whole subtract
S
So we need to find the sum of the whole tree first T
Because I don't know which node is the root of the first tree
So any node needs Traverse
To
bring S * (T - S)
Maximum
Simultaneous recording Maximum
value
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 maxProduct(_ root: TreeNode?) -> Int {
// result
var res: Int = Int.min
// The sum of the
var allSum: Int = 0
// Part I molecular tree and
var nodeSum: Int = 0
// Sum up
func sum(_ root: TreeNode?) -> Int {
guard let r = root else { return 0 }
return r.val + sum(r.left) + sum(r.right)
}
// Recursively find subtrees and
func dfs(_ root: TreeNode?) -> Int {
guard let r = root else { return 0 }
// Subtree and
nodeSum = r.val + dfs(r.left) + dfs(r.right)
// Calculate the maximum
res = max(res, (allSum - nodeSum) * nodeSum)
return nodeSum
}
allSum = sum(root)
_ = dfs(root)
res = res % (Int(1e9) + 7)
return res
}
0x03 My little work
Welcome to experience one of my works : Little notes -XNote
Notes are a good helper ~
One step in place App Store
Search ~
边栏推荐
- Automatic invoice processing - get rid of the shackles of paper and data input, automate workflow and exception handling, and significantly shorten the audit preparation time
- 【C语言】文件相关操作
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-2 personal solutions
- [kaggle] how to effectively avoid oom (out of memory) and long alchemy process
- Mysql的主键UUID、自增ID、雪花算法到底该怎么选择?(荣耀典藏版)
- DAM-第十三章(数据质量管理)
- [untitled]
- leetcode:407. Connected to rainwater II
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-1 personal solutions
- Yunna Xianning communication machine room dynamic loop monitoring system, telecom dynamic loop monitoring system
猜你喜欢
软链接和硬链接的区别以及文件系统如何取文件
SQL: SELECT t.`app_code`, SUM(t.`success_num`) AS success_num, SUM(t.`
微带线拉开7H你就觉得很稳了?
[200 opencv routines] 235 Principal component analysis for feature extraction (sklearn)
Audience analysis and uninstall analysis have been comprehensively upgraded, and HMS core analysis service version 6.6.0 has been updated
Okaleido tiger NFT即将登录Binance NFT平台,后市持续看好
From fail to pass, what did DDR debugging go through?
Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
从去IOE到CIPU,中国云计算要走出自己的路径
[JS] console command you don't know
随机推荐
【C语言】文件相关操作
Intel汇编语言程序设计学习-第五章 过程-上
金仓数据库KingbaseES数据库管理员指南--17数据库调度概念
当删则删,这种电容本不该出现
金仓数据库KingbaseES数据库管理员指南--15.3. 管理同义词
据说只有大神才知道这个电容的作用
HVV蓝队之入侵排查
leetcode:407. 接雨水 II
Rust language - tuple
networkX 可视化图 visibility graph 如何根据时间序列得到图
带你认识一下数仓的分区自动管理
【小程序】快来开发你的第一个微信小游戏(详细流程)
【硬件基础4】二极管(原理、特性、类型、电路分析)
From fail to pass, what did DDR debugging go through?
When a PCB designer meets love, guess how much the impedance in his board changes
金仓数据库KingbaseES数据库管理员指南--15.2. 管理序列
Lora技术助力冷链发展
想请教下标准模式下,生产环境的sql节点能对routine_sql_test_tianyi进行sel
当PCB设计师遇到爱情,猜猜他板内的阻抗有多大变化
Contract development using rust on Solana