/**
* 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:
int sum = 0;
void track_back(TreeNode* cur , string s)
{
if(cur == nullptr ) return;
s += to_string(cur->val);
if(cur->left == nullptr && cur->right ==nullptr) sum += stoi(s);
track_back(cur->left , s);
track_back(cur->right ,s);
return;
}
int sumNumbers(TreeNode* root) {
string s;
track_back(root,s);
return sum;
}
};