add: 220515 [cpp]
This commit is contained in:
parent
3f534be3dd
commit
96559bcf3e
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include <queue>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
struct TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode* left;
|
||||||
|
TreeNode* right;
|
||||||
|
|
||||||
|
explicit TreeNode(int x = 0, TreeNode* l = nullptr, TreeNode* r = nullptr) : val(x), left(l), right(r) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1302. Deepest Leaves Sum
|
||||||
|
* Given the root of a binary tree, return the sum of values of its deepest leaves.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static int deepestLeavesSum(TreeNode* r) {
|
||||||
|
if (!r) return 0;
|
||||||
|
std::queue<std::pair<const TreeNode*, int>> q;
|
||||||
|
int ret = 0, l = 0;
|
||||||
|
q.push({r, 0});
|
||||||
|
while (!q.empty()) {
|
||||||
|
const auto [n, d] = q.front();
|
||||||
|
q.pop();
|
||||||
|
ret = (d == l ? ret : 0) + n->val;
|
||||||
|
l = d;
|
||||||
|
if (n->left) q.push({n->left, 1 + d});
|
||||||
|
if (n->right) q.push({n->right, 1 + d});
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2205)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2205 220514.cpp)
|
ADD_EXECUTABLE(2205 220515.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue