一、题目
二、代码
/**
* 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:
vector<int> path;
int return_int=0;
int calculate_sum(vector<int> process_vector )
{
int i;
int temp_sum=0;
for(i=0;i<process_vector.size();i++) temp_sum=temp_sum*10+process_vector[i];
return temp_sum;
}
void recursion(TreeNode* process_node)
{
if(process_node->left==nullptr&&process_node->right==nullptr)
{
return_int=return_int+calculate_sum(path);
}
if(process_node->left)
{
path.push_back(process_node->left->val);
recursion(process_node->left);
path.pop_back();
}
if(process_node->right)
{
path.push_back(process_node->right->val);
recursion(process_node->right);
path.pop_back();
}
}
int sumNumbers(TreeNode* root)
{
path.clear();
if(root==nullptr) return 0;
path.push_back(root->val);
recursion(root);
return return_int;
}
};