-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path24.cpp
More file actions
70 lines (63 loc) · 1.23 KB
/
24.cpp
File metadata and controls
70 lines (63 loc) · 1.23 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
69
70
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int> > res;
vector<int> trace;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode() {}
};
TreeNode* newTree()
{
TreeNode* node = new TreeNode;
int x;
cin >> x;
if (!x)node = NULL;
else
{
node->val = x;
node->left = newTree();
node->right = newTree();
}
return node;
}
void dfs(TreeNode* node, int surplus)
{
trace.push_back(node->val);
if (node->val == surplus && !node->left && !node->right)
res.push_back(trace);
if (node->left)dfs(node->left, surplus - node->val);
if (node->right)dfs(node->right, surplus - node->val);
trace.pop_back();
}
vector<vector<int> > FindPath(TreeNode* root, int expectNumber)
{
if (root)dfs(root, expectNumber);
return res;
}
int main()
{
ios::sync_with_stdio(false);
TreeNode* root = NULL;
root = newTree();
int n;
cin >> n;
vector<vector<int> > ans;
vector<int> temp;
ans = FindPath(root, n);
vector<vector<int> >::iterator it;
vector<int>::iterator ite;
for (it = ans.begin(); it != ans.end(); it++)
{
temp = *it;
for (ite = temp.begin(); ite != temp.end(); ite++)
cout << *ite << " ";
cout << endl;
}
cout << endl;
return 0;
}