-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path129.SumRootToLeafNumbers.cpp
More file actions
30 lines (22 loc) · 927 Bytes
/
129.SumRootToLeafNumbers.cpp
File metadata and controls
30 lines (22 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<bits/stdc++.h>
using namespace std;
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 sumNumbers(TreeNode* root,int sum=0)
{
if(!root) // check if the root is empty or not for being null
return 0;
sum=(sum*10)+(root->val); // take out the digit number from root till end child
if(!root->left && !root->right) // check if the root's left and right not empty if so we return the digit made till there for sum
return sum;
return sumNumbers(root->left,sum)+sumNumbers(root->right,sum); // check same for the left and right side
}
};