Skip to content

Commit 0bf861b

Browse files
authored
Create 129. Sum Root to Leaf Numbers1 (#457)
2 parents 91d7167 + d72bd91 commit 0bf861b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

129. Sum Root to Leaf Numbers1

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
4+
void dfs(TreeNode *root, ll &ans , ll tmp){
5+
if(root->left== NULL && root->right==NULL){
6+
tmp = tmp*10 + root->val;
7+
ans+=tmp;
8+
return;
9+
}
10+
tmp = tmp*10 + root->val;
11+
if(root->left) dfs(root->left,ans,tmp);
12+
if(root->right) dfs(root->right,ans,tmp);
13+
}
14+
15+
int sumNumbers(TreeNode* root) {
16+
ll ans =0;
17+
dfs(root,ans,0);
18+
return int(ans);
19+
}
20+
};

0 commit comments

Comments
 (0)