加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift]LeetCode1161. 最大层内元素和 | Maximum Level Sum of a

发布时间:2020-12-14 04:25:53 所属栏目:百科 来源:网络整理
导读:★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ?微信公众号:为敢(WeiGanTechnologies) ?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/) ?GitHub地址:https://github.com/strengthen/LeetCode ?原文

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公众号:为敢(WeiGanTechnologies)
?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/11371957.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.

Return the?smallest?level?X?such that the sum of all the values of nodes at level?X?is?maximal.

Example 1:

Input: [1,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. The number of nodes in the given tree is between?1?and?10^4.
  2. -10^5 <= node.val <= 10^5

给你一个二叉树的根节点?root。设根节点位于二叉树的第?1?层,而根节点的子节点位于第?2?层,依此类推。

请你找出层内元素之和?最大?的那几层(可能只有一层)的层号,并返回其中?最小?的那个。

示例:

输入:[1,null]
输出:2
解释:
第 1 层各元素之和为 1,
第 2 层各元素之和为 7 + 0 = 7,
第 3 层各元素之和为 7 + -8 = -1,
所以我们返回第 2 层的层号,它的层内元素之和最大。

提示:

  1. 树中的节点数介于?1?和?10^4?之间
  2. -10^5 <= node.val <= 10^5

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读