-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path4_CheckBST.cpp
136 lines (122 loc) · 3.07 KB
/
4_CheckBST.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <bits/stdc++.h>
#include "BinaryTreeNode.cpp"
using namespace std;
void preOrderPrint(BinaryTreeNode<int> *root)
{
if (root == NULL)
{
return;
}
cout << root->data << " ";
preOrderPrint(root->left);
preOrderPrint(root->right);
}
/**
Coding Problem
Problem Statement: Check BST
Problem Level: MEDIUM
Problem Description:
Given a binary tree with N number of nodes, check if that input tree is BST (Binary Search Tree) or not. If yes, return true, return false otherwise.
Duplicate elements should be in right subtree.
Input format :
Line 1 : Nodes in level order form (separated by space). If any node does not have left or right child, take -1 in its place
Output format :
true or false
Constraints :
1 <= N <= 10^5
Sample Input 1 :
3 1 5 -1 2 -1 -1 -1 -1
Sample Output 1 :
true
Sample Input 2 :
5 2 10 0 1 -1 15 -1 -1 -1 -1 -1 -1
Sample Output 2 :
false
*/
// Approach 1 - O(n*h) i.e. O(nlogn) if tree balanced , O(n^2) if tree height is O(n)
int maximum(BinaryTreeNode<int> *root)
{
if (root == NULL)
{
return INT_MIN;
}
return max(root->data, max(maximum(root->left), maximum(root->right)));
}
int minimum(BinaryTreeNode<int> *root)
{
if (root == NULL)
{
return INT_MAX;
}
return min(root->data, min(minimum(root->left), minimum(root->right)));
}
bool isBST(BinaryTreeNode<int> *root)
{
if (root == NULL)
{
return true;
}
int leftMax = maximum(root->left);
int rightMin = minimum(root->right);
bool ans = (root->data > leftMax) && (root->data <= rightMin) && isBST(root->left) && isBST(root->right);
return ans;
}
// Approach 2 - O(n) - one pass solution
class HelperBST
{
public:
int min;
int max;
bool isBST;
};
HelperBST isBST2Helper(BinaryTreeNode<int> *root)
{
if (root == NULL)
{
HelperBST ans;
ans.min = INT_MAX;
ans.max = INT_MIN;
ans.isBST = true;
return ans;
}
HelperBST left = isBST2Helper(root->left);
HelperBST right = isBST2Helper(root->right);
HelperBST ans;
ans.min = min(root->data, min(left.min, right.min));
ans.max = max(root->data, max(left.max, right.max));
ans.isBST = (root->data > left.max) && (root->data <= right.min) && left.isBST && right.isBST;
return ans;
}
bool isBST2(BinaryTreeNode<int> *root)
{
HelperBST ans = isBST2Helper(root);
return ans.isBST;
}
// Approach 3 - O(n)
bool isBST3Helper(BinaryTreeNode<int> *root, int min = INT_MIN, int max = INT_MAX)
{
if (root == NULL)
{
return true;
}
if (root->data < min || root->data > max)
{
return false;
}
bool left = isBST3Helper(root->left, min, root->data - 1);
bool right = isBST3Helper(root->right, root->data, max);
return left && right;
}
bool isBST3(BinaryTreeNode<int> *root)
{
bool ans = isBST3Helper(root);
return ans;
}
int main()
{
BinaryTreeNode<int> *root = taktInputLevelorder();
cout << (isBST(root) ? "true" : "false");
cout << (isBST2(root) ? "true" : "false");
cout << (isBST3(root) ? "true" : "false");
return 0;
}