1
class Solution {
2
public:
3
int ans = 0;
4
void dfs(TreeNode *root, string s) {
5
if (!root->left && !root->right) {
6
s += to_string(root->val);
7
ans += stoi(s);
8
return;
9
}
10
string o = s;
11
s += to_string(root->val);
12
if (root->left) dfs(root->left, s);
13
if (root->right) dfs(root->right, s);
14
s = o;
15
}
16
int sumNumbers(TreeNode *root) {
17
if (!root) return ans;
18
string s = "";
19
dfs(root, s);
20
return ans;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0