★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-gnqfvfoj-km.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given the root
of a binary tree, the level of its root is 1
, the level of its children is 2
, and so on.node
Return the smallest level X
such that the sum of all the values of nodes at level X
is maximal.git
Example 1:github
Input: [1,7,0,7,-8,null,null]
Output: 2 Explanation: Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + -8 = -1. So we return the level with the maximum sum which is level 2.
Note:微信
1
and 10^4
.-10^5 <= node.val <= 10^5
給你一個二叉樹的根節點 root
。設根節點位於二叉樹的第 1
層,而根節點的子節點位於第 2
層,依此類推。app
請你找出層內元素之和 最大 的那幾層(可能只有一層)的層號,並返回其中 最小 的那個。spa
示例:code
輸入:[1,7,0,7,-8,null,null] 輸出:2 解釋: 第 1 層各元素之和爲 1, 第 2 層各元素之和爲 7 + 0 = 7, 第 3 層各元素之和爲 7 + -8 = -1, 因此咱們返回第 2 層的層號,它的層內元素之和最大。
提示:htm
1
和 10^4
之間-10^5 <= node.val <= 10^5
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 var levels: [Int] = [] 16 func maxLevelSum(_ root: TreeNode?) -> Int { 17 traverse(root, 0) 18 var maxValue = root!.val 19 var maxValueIndex = 0 20 for (index, level) in levels.enumerated() { 21 if level > maxValue { 22 maxValue = level 23 maxValueIndex = index 24 } 25 } 26 return maxValueIndex + 1 27 } 28 29 func traverse(_ node: TreeNode?, _ level: Int) { 30 if let node = node { 31 if level >= levels.count { 32 levels.append(0) 33 } 34 levels[level] += node.val 35 traverse(node.left, level + 1) 36 traverse(node.right, level + 1) 37 } 38 } 39 }
764msblog
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func maxLevelSum(_ root: TreeNode?) -> Int { 16 var levels: [Int: Int] = [:] 17 dfs(node: root, depth: 1, levels: &levels) 18 var result = 0 19 var maximum = levels[0] ?? Int.min 20 for level in levels { 21 if maximum < (level.value ?? Int.min) { 22 // new minimum 23 maximum = (level.value ?? Int.min) 24 result = level.key 25 } 26 } 27 28 return result 29 } 30 31 private func dfs(node: TreeNode?, depth: Int, levels: inout [Int: Int]) { 32 guard let node = node else { return } 33 34 levels[depth] = (levels[depth] ?? 0) + node.val 35 36 dfs(node: node.left, depth: depth + 1, levels: &levels) 37 dfs(node: node.right, depth: depth + 1, levels: &levels) 38 } 39 }
808ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 var maxs = [Int](repeating: 0, count: 10001) 16 func maxLevelSum(_ root: TreeNode?) -> Int { 17 helper(root, 1) 18 return maxs.enumerated().max { (a, b) -> Bool in 19 a.element <= b.element 20 }!.offset 21 } 22 23 func helper(_ node: TreeNode?, _ lvl: Int) { 24 guard let n = node else { 25 return 26 } 27 maxs[lvl] += n.val 28 helper(n.right, lvl + 1) 29 helper(n.left, lvl + 1) 30 } 31 }
Runtime: 828 ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 var s:[Int:Int] = [Int:Int]() 16 func maxLevelSum(_ root: TreeNode?) -> Int { 17 dfs(root,1) 18 var best:Int = s[1,default:0] 19 var v:Int = 1 20 for (key,val) in s 21 { 22 if val > best 23 { 24 best = val 25 v = key 26 } 27 } 28 return v 29 } 30 31 func dfs(_ root: TreeNode?,_ level:Int) 32 { 33 if root == nil {return} 34 dfs(root!.left, level + 1) 35 dfs(root!.right, level + 1) 36 s[level,default:0] += root!.val 37 } 38 }