-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAY 29 Validate Binary Search Tree.cpp
58 lines (45 loc) · 1.27 KB
/
DAY 29 Validate Binary Search Tree.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
//Validate Binary Search Tree .cpp problem 1
class Solution {
public:
bool Valid(TreeNode* node,long min,long max) {
if(!node)
return true;
if(node ->val <max && node ->val >min )
return Valid(node->left,min, node->val) && Valid(node ->right,node->val,max);
return false;
}
bool isValidBST(TreeNode* root)
{ return Valid(root,LONG_MIN,LONG_MAX);
}
};
//Two Sum IV - Input is a BST problem 2
class Solution {
public:
unordered_set<int>s;
bool findTarget(TreeNode* root, int k) {
if(root==NULL)
return false;
if(s.count(k-root->val))
return true;
s.insert(root->val);
return findTarget(root->left,k)|| findTarget(root->right,k);
}
};
//Lowest Common Ancestor of a Binary Search Tree problem
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==0)
return root;
while(root!=0)
{
if(p->val>root->val and q->val >root->val)
root=root->right;
else if(p->val < root->val and q->val <root->val)
root=root->left;
else
return root;
}
return root;
}
};