-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo-sum-iv-input-is-a-bst.cpp
84 lines (60 loc) · 1.99 KB
/
two-sum-iv-input-is-a-bst.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
//这个解法感觉也挺丑的,参考的是这个 https://www.acwing.com/solution/LeetCode/content/207/
//看看人家https://leetcode.com/problems/two-sum-iv-input-is-a-bst/discuss/106059/JavaC++-Three-simple-methods-choose-one-you-like
vector<int> vec;
bool findTarget(TreeNode* root, int k) {
inorder(root);
return binary_seach(k);
}
void inorder(TreeNode* root){
if(!root) return ;
if(root->left) inorder(root->left);
vec.push_back(root->val);
if(root->right) inorder(root->right);
}
// 注意这个二分和我熟悉的优点不一样
bool binary_seach(int k){
int l=0,r=vec.size()-1;
while(l!=r){
if(vec[l]+vec[r]==k) return true;
if(vec[l]+vec[r]<k) l++;
else r--;
}
return false;
}
//最暴力的解法,针对每个节点都重新搜索一遍树。
unordered_map<TreeNode*,bool> um;
TreeNode* head;
bool findTarget1(TreeNode* root, int k) {
head=root;
return findfirst(root,k);
}
bool findfirst(TreeNode* root, int k) {
if(!root) return false;
um[root]=true;
if(findsecond(head,k-root->val)) return true;
um[root]=false;
if(root->left && findfirst(root->left,k)) return true;;
if(root->right && findfirst(root->right,k)) return true;
return false;
}
bool findsecond(TreeNode* root, int k){
if(!root) return false;
if(root->val==k){
if(!um[root]) return true;
return false;
}
if(root->val>k) return findsecond(root->left,k);
return findsecond(root->right,k);
}
};