-
Notifications
You must be signed in to change notification settings - Fork 297
/
TwoSum.cpp
32 lines (26 loc) · 874 Bytes
/
TwoSum.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
/*
// Structure of the Tree node.
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
}
*/
bool findPair(TreeNode * root, int k){
unordered_set<int> values; // to store the values
return dfs(root, k, values);
}
bool dfs(TreeNode* root, int k, unordered_set<int> &values){
// If complement not found beyond the leaf node, then return false;
if(root == NULL) return false;
// Find the complement of the current node in the hash table.
// If found complement, then return true.
if(values.find(k - root -> val) != values.end()){
return true;
}
//Insert the current node value in hashtable.
values.insert(root -> val);
// Same process for node in left and right subtrees.
return dfs(root -> left, k, values) ||
dfs(root -> right, k, values);
}