Skip to content

Commit 4591228

Browse files
Merge pull request #6 from Praddyumn16/patch-3
Created leetcode.com-explore-challenge-card-september-leetcoding-chall…
2 parents 662e8bc + 1cba589 commit 4591228

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
int sum1(TreeNode* root , int sum){
14+
if(!root) return 0;
15+
sum = (sum << 1) + root->val;
16+
if(!root->right && !root->left) return sum;
17+
return sum1(root->left , sum) + sum1(root->right , sum);
18+
}
19+
public:
20+
int sumRootToLeaf(TreeNode* root) {
21+
return sum1(root , 0);
22+
}
23+
};
24+
25+

0 commit comments

Comments
 (0)