-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.二叉树中和为某一值的路径.cpp
More file actions
33 lines (30 loc) · 942 Bytes
/
25.二叉树中和为某一值的路径.cpp
File metadata and controls
33 lines (30 loc) · 942 Bytes
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
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
vector<vector<int> > res;
if(root == NULL) return res;
vector<int> path;
int sum = 0;
find(root, res, path, sum, expectNumber);
return res;
}
private:
void find(TreeNode *root, vector<vector<int> > &res, vector<int> &path, int sum, const int expectNumber){
path.push_back(root->val);
sum += root->val;
bool isLeaf = (root->left == NULL) && (root->right == NULL);
if(isLeaf && sum == expectNumber) res.push_back(path);
if(root->left) find(root->left, res, path, sum, expectNumber);
if(root->right) find(root->right, res, path, sum, expectNumber);
path.pop_back();
}
};