Skip to content

Commit f124f7e

Browse files
committed
cpp
1 parent d4e6f76 commit f124f7e

File tree

1 file changed

+20
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)