-
Notifications
You must be signed in to change notification settings - Fork 0
/
200209-1.cpp
59 lines (55 loc) · 1.1 KB
/
200209-1.cpp
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
57
58
59
// https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/
#include <cstdio>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void release(TreeNode* t)
{
if (t) {
release(t->left);
release(t->right);
delete t;
}
}
class Solution {
public:
int sumNumbers(TreeNode* root) {
int sum = 0;
if (root) sumNumbers(root, 0, sum);
return sum;
}
private:
void sumNumbers(TreeNode* t, int prefix, int& sum) {
int val = prefix * 10 + t->val;
if (!t->left && !t->right) {
sum += val;
} else {
if (t->left) sumNumbers(t->left, val, sum);
if (t->right) sumNumbers(t->right, val, sum);
}
}
};
int main()
{
Solution s;
{
TreeNode* t = new TreeNode(1);
t->left = new TreeNode(2);
t->right = new TreeNode(3);
printf("%d\n", s.sumNumbers(t)); // answer: 25
release(t);
}
{
TreeNode* t = new TreeNode(4);
t->left = new TreeNode(9);
t->right = new TreeNode(0);
t->left->left = new TreeNode(5);
t->left->right = new TreeNode(1);
printf("%d\n", s.sumNumbers(t)); // answer: 1026
release(t);
}
return 0;
}