-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepest-leaves-sum.cpp
More file actions
44 lines (34 loc) · 876 Bytes
/
Copy pathdeepest-leaves-sum.cpp
File metadata and controls
44 lines (34 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// Created by darion.yaphet on 2025/5/8.
//
#include "TreeNode.h"
#include <queue>
using namespace std;
// https://leetcode.cn/problems/deepest-leaves-sum/
class Solution {
public:
int deepestLeavesSum(TreeNode *root) {
if (root == nullptr) {
return 0;
}
queue<TreeNode *> q;
q.push(root);
int result = 0;
while (!q.empty()) {
int size = q.size();
result = 0;
for (int i = 0; i < size; ++i) {
TreeNode *node = q.front();
q.pop();
if (node->left != nullptr) {
q.push(node->left);
}
if (node->right != nullptr) {
q.push(node->right);
}
result += node->val;
}
}
return result;
}
};