-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatedSUbString.cpp
More file actions
56 lines (43 loc) · 1.45 KB
/
RepeatedSUbString.cpp
File metadata and controls
56 lines (43 loc) · 1.45 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int numRootToLeaf(TreeNode* root, int currSum){
if(!root) return 0;
if(!root->left && !root->right) return currSum*10+root->val;
return numRootToLeaf(root->left , currSum*10 + root->val) + numRootToLeaf(root->right , currSum*10 + root->val) ;
}
int suumNumbers(TreeNode* root) {
if(!root) return 0;
return numRootToLeaf(root, 0);
}
//Using STACk AND DFS
int sumNumbers(TreeNode* root) {
if(!root) return 0;
int maxSum =0;
stack<pair<TreeNode*, int>> S;
S.push(make_pair(root, root->val));
int res=0;
while(!S.empty()){
TreeNode* node = S.top().first;
int sum = S.top().second;
S.pop();
if(!node->left && !node->right) {
res +=sum;
continue;
}
if(node->right)
S.push(make_pair(node->right, sum*10 + node->right->val));
if(node->left)
S.push(make_pair(node->left, sum*10 + node->left->val));
}
return res;
}
};