当前位置:网站首页>最大层内元素和
最大层内元素和
2022-08-02 02:26:00 【小小草帽】
题目
给你一个二叉树的根节点 root。设根节点位于二叉树的第 1 层,而根节点的子节点位于第 2 层,依此类推。
请返回层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那个。
示例
示例 1:
输入:root = [1,7,0,7,-8,null,null]
输出:2
解释:
第 1 层各元素之和为 1,
第 2 层各元素之和为 7 + 0 = 7,
第 3 层各元素之和为 7 + -8 = -1,
所以我们返回第 2 层的层号,它的层内元素之和最大。
示例 2:
输入:root = [989,null,10250,98693,-89388,null,null,null,-32127]
输出:2
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法:
BFS:利用广度优先遍历逐层计算每一层的和,然后返回和最大的层的编号。
DFS:创建数组存储每一层的和,利用深度优先遍历计算每层和。
代码
BFS
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxLevelSum(self, root: Optional[TreeNode]) -> int:
queue =[root]
res = float("-inf")
number = 0
piece = 0
while queue:
n = len(queue)
ans = 0
number += 1
for _ in range(n):
node = queue.pop(0)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
ans += node.val
if ans > res:
piece = number
res = ans
return piece
DFS
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxLevelSum(self, root: Optional[TreeNode]) -> int:
sum = []
def dfs(node, level):
if level == len(sum):
sum.append(node.val)
else:
sum[level] += node.val
if node.left:
dfs(node.left, level + 1)
if node.right:
dfs(node.right, level + 1)
dfs(root, 0)
return sum.index(max(sum)) + 1
边栏推荐
- 2022年NPDP考完多久出成绩?怎么查询?
- 欧拉公式的证明
- PHP live source code to achieve simple barrage effect related code
- 使用self和_(下划线)的区别
- Analysis of the status quo of digital transformation of manufacturing enterprises
- Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
- How engineers treat open source
- swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
- Check if IP or port is blocked
- Outsourcing worked for three years, it was abolished...
猜你喜欢

2022河南青训联赛第(三)场

nacos启动报错,已配置数据库,单机启动

2022 Henan Youth Training League Game (3)

Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒

The state status is displayed incorrectly after the openGauss switch

Golang分布式应用之定时任务

使用DBeaver进行mysql数据备份与恢复

How to adjust the cross cursor too small, CAD dream drawing calculation skills

qt点云配准软件

The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
随机推荐
项目后台技术Express
【web】Understanding Cookie and Session Mechanism
记一个gorm初始化的坑
NAS和私有云盘的区别?1篇文章说清楚
通用客户端架构
机器人领域期刊会议汇总
Oracle19c安装图文教程
to-be-read list
2022 Henan Youth Training League Game (3)
cocos中使用async await异步加载资源
记一次gorm事务及调试解决mysql死锁
Can Youxuan database import wrongly be restored?
Personal blog system project test
The failure to create a role in Dahua Westward Journey has been solved
Unable to log in to the Westward Journey
Ringtone 1161. Maximum In-Layer Elements and
LeetCode 213. Robbery II (2022.08.01)
AI target segmentation capability for fast video cutout without green screen
线程的不同状态
¶ Backtop back to the top is not effective