-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_sum_check_in_BST.cpp
58 lines (51 loc) · 1.3 KB
/
two_sum_check_in_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
#include <iostream>
using namespace std;
/*question -> To check if any two nodes sum in the bst will equal to target or not.*/
class Node
{
public:
int data;
Node *left, *right;
Node(int val)
{
this->data = val;
left = right = NULL;
}
};
/* concept -> Do the inorder traversal and store it in the vector, as we know that the inorder traversal of the bst is sorted.
Then apply the two pointer approach on the vector and check if there exist any pair or not.*/
void inorder(Node *root, vector<int> &store)
{
if (root == NULL)
return;
// left node
inorder(root->left, store);
// node or root
store.push_back(root->data);
// right node
inorder(root->right, store);
}
bool checkSum(Node *root, int target)
{
// do inorder traversal and store it in the vector.
vector<int> store;
inorder(root, store);
// now store contains the sorted form of bst nodes.
// now apply the two pointer approach on the store vector.
int left = 0, right = store.size() - 1;
while (left < right)
{
int sum = store[left] + store[right];
if (sum == target)
return true;
else if (sum > target)
right--;
else
left++;
}
return false;
}
int main()
{
return 0;
}