-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path129.cpp
65 lines (61 loc) · 1.84 KB
/
129.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
60
61
62
63
64
65
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//直观感觉使用深度优先算法dfs,递归
//这是我的复杂办法,甚至递归得有点乱(有点像113题的回溯问题)
/*class Solution {
public:
int sumNumbers(TreeNode* root) {
if (!root) {
return 0;
}
string str;
return helper(root, str);
}
private:
int helper(TreeNode *root, string &str) {
if (!root) {
return 0;
}
str.push_back(root->val + '0');
if (!root->left && !root->right) {
int ret = atoi(str.c_str());
str.erase(str.end() - 1) ;//从str里删除最近push_back的元素
return ret;
}
int ret_int = helper(root->left, str) + helper(root->right, str);
str.erase(str.end() - 1) ;//从str里删除最近push_back的元素
return ret_int;
}
};*/
//这是大部分人的做法,这个做法比较好,递归里没有用到引用,不会使代码变得复杂
//没用string,而是直接计算结果,用cur_val参数来保存该结点之前结点若为叶子结点时的总值
class Solution {
public:
int sumNumbers(TreeNode* root) {
//if (!root) {
// return 0;
//}
//if (!root->left && !root->right) {
// return root->val;
//}
return helper(root, 0);
}
private:
int helper(TreeNode *root, int cur_val) {
if (!root) {
return 0;
}
cur_val = root->val + cur_val * 10;
if (!root->left && !root->right) {
return cur_val;
}
return helper(root->left, cur_val) + helper(root->right, cur_val);
}
};