-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1302.层数最深叶子节点的和.cpp
62 lines (54 loc) · 1.46 KB
/
1302.层数最深叶子节点的和.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* @lc app=leetcode.cn id=1302 lang=cpp
*
* [1302] 层数最深叶子节点的和
*/
#include <iostream>
#include <queue>
using namespace std;
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) {}
};
// @lc code=start
class Solution {
public:
// 6 48;
// 层次遍历 队列
int deepestLeavesSum(TreeNode* root) {
if(!root){
return 0;
}
queue<TreeNode*> q; // push front pop
int countPre = 0, count = 0;
int sum = 0;
q.push(root);
countPre++;
while(!q.empty()){
TreeNode *p = q.front();
q.pop();
if(countPre == 0){ // p是新层第一个
sum = 0;
countPre = count - 1; // 上一层还有 count-1 个未出队
count = 0; // 新层开始统计个数
}else{
countPre--;
}
sum += p->val;
if(p->left){
q.push(p->left);
count++;
}
if(p->right){
q.push(p->right);
count++;
}
}
return sum;
}
};
// @lc code=end