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