-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPathInTree.cpp
More file actions
28 lines (28 loc) · 1000 Bytes
/
PathInTree.cpp
File metadata and controls
28 lines (28 loc) · 1000 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
class Solution {
public:
vector<vector<int>> res;
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
if (root == NULL)
return res;
vector<int> path;
dfs(root,path,expectNumber);
return res;
}
void dfs(TreeNode* root, vector<int>& path, int expectNumber)
{
if (root == NULL)
return;
path.push_back(root->val);
// 如果是叶节点,并且路径上的节点值的和为输入的值,就像结果中添加这一path
bool isLeaf = (root->left == NULL && root->right == NULL);
if (expectNumber == root->val && isLeaf)
{
res.push_back(path);
}
// 不是叶节点就遍历他的子节点
dfs(root->left, path, expectNumber-root->val);
dfs(root->right, path, expectNumber-root->val);
// 到这一步说明不满足要求,要返回父节点,需要删除路径上的当前节点
path.pop_back();
}
};