-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path653_twoSumIV.cpp
More file actions
56 lines (51 loc) · 1.24 KB
/
Copy path653_twoSumIV.cpp
File metadata and controls
56 lines (51 loc) · 1.24 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<int> travelTree(TreeNode* root)
{
vector<int> res;
if (root == NULL)
return res;
res = travelTree(root->left);
res.push_back(root->val);
vector<int> rightChild = travelTree(root->right);
res.insert(res.end(), rightChild.begin(), rightChild.end());
return res;
}
bool findNum(TreeNode* root, int num)
{
if (root == NULL)
return false;
else if (root->val == num)
return true;
else if (root->val > num)
return findNum(root->left, num);
else
return findNum(root->right, num);
}
bool findTarget(TreeNode* root, int k) {
if (root == NULL)
return false;
vector<int> nums=travelTree(root);
for (size_t i=0; i<nums.size(); ++i)
{
if ( k!=nums[i]*2 && findNum(root, k-nums[i]) )
return true;
}
return false;
}
};
int main()
{
Solution solution;
return 0;
}