Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-06-15 10:45:06
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/description/
1161. Maximum Level Sum of a Binary Tree
返回最大的总和层数(每层),如果总和相同取较小的。
Example 1:
https://assets.leetcode.com/uploads/2019/05/03/capture.JPG
Input: root = [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.
Example 2:
Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
Output: 2
思路:
1.用BFS去遍历,每次算出来的都是当前层数和,如果当前和大于前面的才更新结果。
Java Code:
作者: DDFox (冒险者兼清洁工)   2023-06-15 10:46:00
大师
作者: JIWP (JIWP)   2023-06-15 10:49:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com