-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathInTree.cpp
More file actions
68 lines (58 loc) · 1.5 KB
/
PathInTree.cpp
File metadata and controls
68 lines (58 loc) · 1.5 KB
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
/*
剑指Offer
面试题34:二叉树中和为某一值的路径
题目:输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。
从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
*/
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
vector<int> path;
dfs(root, sum, res, path);
return res;
}
private:
void dfs(TreeNode* root, int sum, vector<vector<int>>& res, vector<int>& path) {
if (root == nullptr)
return;
path.push_back(root->val);
if (root->left == nullptr && root->right == nullptr && sum == root->val)
res.push_back(path);
dfs(root->left, sum - root->val, res, path);
dfs(root->right, sum - root->val, res, path);
path.pop_back();
}
};
TreeNode* createBinaryTree() {
int x;
cin >> x;
if (x == 0)
return nullptr;
TreeNode* root = new TreeNode(x);
root->left = createBinaryTree();
root->right = createBinaryTree();
return root;
}
int main()
{
Solution solution;
TreeNode* root = createBinaryTree();
cout << "二叉树创建完成" << endl;
vector<vector<int>> res = solution.pathSum(root, 22);
for (auto v : res) {
for (auto x : v)
cout << x << '\t';
cout << '\n';
}
return 0;
}