add: 230314
This commit is contained in:
parent
ccb2bb4385
commit
a8e062f2b6
|
|
@ -0,0 +1,46 @@
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
struct TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode* left;
|
||||||
|
TreeNode* right;
|
||||||
|
|
||||||
|
explicit TreeNode(int x, TreeNode* left = nullptr, TreeNode* right = nullptr) : val(x), left(left), right(right) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 129. Sum Root to Leaf Numbers
|
||||||
|
*
|
||||||
|
* You are given the root of a binary tree containing digits from 0 to 9 only.
|
||||||
|
* Each root-to-leaf path in the tree represents a number.
|
||||||
|
* For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
|
||||||
|
* Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
|
||||||
|
* A leaf node is a node with no children.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static int sumNumbers(const TreeNode* root) {
|
||||||
|
if (!root)
|
||||||
|
return 0;
|
||||||
|
int stack[16]{root->val}, ptr = 0, ans = 0;
|
||||||
|
std::function<void(const TreeNode*)> dfs = [&](const TreeNode* r) {
|
||||||
|
if (!r->left && !r->right) {
|
||||||
|
ans += stack[ptr];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
++ptr;
|
||||||
|
if (r->left) {
|
||||||
|
stack[ptr] = 10 * stack[ptr - 1] + r->left->val;
|
||||||
|
dfs(r->left);
|
||||||
|
}
|
||||||
|
if (r->right) {
|
||||||
|
stack[ptr] = 10 * stack[ptr - 1] + r->right->val;
|
||||||
|
dfs(r->right);
|
||||||
|
}
|
||||||
|
--ptr;
|
||||||
|
};
|
||||||
|
dfs(root);
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2303)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2303 230314-CN.cpp)
|
ADD_EXECUTABLE(2303 230314.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue