1161. Maximum Level Sum of a Binary Tree

更新 發佈閱讀 4 分鐘
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree?envType=daily-question&envId=2026-01-06

我覺得算是好想的問題。一開始看到可能會有些疑惑,但很快應該就能想到可以用 BFS 解。這邊我用來完成 BFS 的是 queue container。透過 queue 的特性來包裝每一個 level 並且適當的 push 與 pop。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxLevelSum(TreeNode* root) {
int max_id = 0, max_sum = INT_MIN;
int cur_id = 0;
int temp;
queue<TreeNode*> tr;
tr.push(root);

while(!tr.empty()) {
cur_id++;
int len = tr.size();
temp = 0;
for(int i = 0; i < len; i++) {
temp += tr.front()->val;
if(tr.front()->left) tr.push(tr.front()->left);
if(tr.front()->right) tr.push(tr.front()->right);
tr.pop();
}
if(temp > max_sum) max_id = cur_id;
max_sum = max(max_sum, temp);
}

return max_id;
}
};

表現的部分 beats 了 60% (浮動很大,大家做法可能都差不多)。有空的話再來看別人的做法。

Best Solution

TODO

留言
avatar-img
星星在晚上的時候不睡覺
0會員
13內容數
資工系的勞碌人生
2026/01/06
https://leetcode.com/problems/longest-valid-parentheses?envType=problem-list-v2&envId=rabvlt31 這題我想在資工系茁壯成長的人一定不陌生,完全就是各種程式課程的回憶。 關於括弧的題目我也做過不少,一開始看
2026/01/06
https://leetcode.com/problems/longest-valid-parentheses?envType=problem-list-v2&envId=rabvlt31 這題我想在資工系茁壯成長的人一定不陌生,完全就是各種程式課程的回憶。 關於括弧的題目我也做過不少,一開始看
2026/01/05
https://leetcode.com/problems/random-pick-with-weight?envType=problem-list-v2&envId=rabvlt31 這題我完全是土法煉鋼,因此跑出來的成績不好不意外哈哈哈。 class Solution { public:
2026/01/05
https://leetcode.com/problems/random-pick-with-weight?envType=problem-list-v2&envId=rabvlt31 這題我完全是土法煉鋼,因此跑出來的成績不好不意外哈哈哈。 class Solution { public:
2026/01/05
https://leetcode.com/problems/maximum-matrix-sum?envType=daily-question&envId=2026-01-05 這一題我願稱之為腦筋急轉彎之騙到我了。 經過了 542 的洗禮後,我看到每個題目都想了想是否可以用 queue 解。這
2026/01/05
https://leetcode.com/problems/maximum-matrix-sum?envType=daily-question&envId=2026-01-05 這一題我願稱之為腦筋急轉彎之騙到我了。 經過了 542 的洗禮後,我看到每個題目都想了想是否可以用 queue 解。這
看更多
你可能也想看
Thumbnail
玉山 Unicard 新戶首刷禮,百大指定消費最高 7.5% 回饋,其中包含日本、韓國、臺灣在地 100 大指定通路,以及國內外旅遊平台、航空公司點數回饋上限1000點。 五大平台每月最高可回饋點數 500 點,今年年底前(12 月底)最後申辦機會,使用期限直達 2026 年 6 月,快把握機會!
Thumbnail
玉山 Unicard 新戶首刷禮,百大指定消費最高 7.5% 回饋,其中包含日本、韓國、臺灣在地 100 大指定通路,以及國內外旅遊平台、航空公司點數回饋上限1000點。 五大平台每月最高可回饋點數 500 點,今年年底前(12 月底)最後申辦機會,使用期限直達 2026 年 6 月,快把握機會!
Thumbnail
許多人為了信用卡優惠,持有大量信用卡,看似精打細算,實則可能浪費時間、造成財務混亂。本文以玉山Unicard為例,探討如何透過整合回饋、簡化選擇,解決多卡族的痛點,實現簡易消費與簡單理財。
Thumbnail
許多人為了信用卡優惠,持有大量信用卡,看似精打細算,實則可能浪費時間、造成財務混亂。本文以玉山Unicard為例,探討如何透過整合回饋、簡化選擇,解決多卡族的痛點,實現簡易消費與簡單理財。
Thumbnail
題目 : 121. Best Time to Buy and Sell Stock
Thumbnail
題目 : 121. Best Time to Buy and Sell Stock
Thumbnail
題目 : 100. Same Tree
Thumbnail
題目 : 100. Same Tree
Thumbnail
題目 : 83. Remove Duplicates from Sorted List
Thumbnail
題目 : 83. Remove Duplicates from Sorted List
Thumbnail
題目 : 69. Sqrt(x)
Thumbnail
題目 : 69. Sqrt(x)
Thumbnail
題目 : 35. Search Insert Position
Thumbnail
題目 : 35. Search Insert Position
Thumbnail
題目 : 28. Find the Index of the First Occurrence in a String
Thumbnail
題目 : 28. Find the Index of the First Occurrence in a String
Thumbnail
題目:66. Plus One
Thumbnail
題目:66. Plus One
Thumbnail
題目 : 9. Palindrome Number
Thumbnail
題目 : 9. Palindrome Number
Thumbnail
題目 : 14. Longest Common Prefix
Thumbnail
題目 : 14. Longest Common Prefix
Thumbnail
題目 : 13. Roman to Integer
Thumbnail
題目 : 13. Roman to Integer
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News