FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-NOTES/Algorithms/113.Path-Sum-II/solution.cpp at master · moranzcw/LeetCode-NOTES · GitHub
moranzcw
/
LeetCode-NOTES
Public
Notifications
You must be signed in to change notification settings
Fork
77
Star
201
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-NOTES
/
Algorithms
/
113.Path-Sum-II
/
solution.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
38 lines (34 loc) · 890 Bytes
Breadcrumbs
LeetCode-NOTES
/
Algorithms
/
113.Path-Sum-II
/
solution.cpp
Copy path
File metadata and controls
38 lines (34 loc) · 890 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
*
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class
Solution
{
public:
vector<vector<
int
> > result;
void
dfs
(TreeNode *node,
int
sum, vector<
int
> curPath,
int
curSum)
{
if
(node ==
NULL
)
return
;
curPath.
push_back
(node->
val
);
if
(node->
left
==
NULL
&& node->
right
==
NULL
)
{
if
(curSum + node->
val
== sum)
result.
push_back
(curPath);
return
;
}
dfs
(node->
left
, sum, curPath, curSum + node->
val
);
dfs
(node->
right
, sum, curPath, curSum + node->
val
);
}
vector<vector<
int
> >
pathSum
(TreeNode *root,
int
sum)
{
vector<
int
> curPath;
dfs
(root,sum,curPath,
0
);
return
result;
}
};
Back
|
FazBrowse Home
|
New Git URL