-
Notifications
You must be signed in to change notification settings - Fork 24
/
113. Path Sum II.cpp
83 lines (75 loc) · 2.05 KB
/
113. Path Sum II.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
113. Path Sum II
Time Space Difficulty
O(n) O(h) Medium
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
*/
/*
*
* 需要注意的是helper 待着reference 走,快很多
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>>result;
helper(root, sum, result, vector<int>() = {});
return result;
}
void helper(TreeNode* root, int sum, vector<vector<int>>& result, vector<int>&cur){
if(!root) {
return;
}
cur.push_back(root->val);
if(!root->right && !root->left && sum == root->val) {
result.push_back(cur);
}
helper(root->left, sum - root->val, result, cur);
helper(root->right,sum - root->val, result, cur);
cur.pop_back();
}
};
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
vector<int> temp;
TreeNode* cur = root, *pre = NULL;
stack<TreeNode*>stk;
while(cur || ! stk.empty()){
if(cur){
sum -= cur->val;
stk.push(cur);
temp.push_back(cur->val);
cur = cur->left;
}else{
cur = stk.top();
if(!cur->left && !cur->right && sum == 0)
res.push_back(temp);
if(cur->right && pre != cur->right)
cur = cur->right;
else{
pre = cur;
sum += cur->val;
cur = NULL;
temp.pop_back();
stk.pop();
}
}
}
return res;
}
};