PROBLEM DESCRIPTION
Given the root of a binary tree. Check whether it is a BST or not. Note: We are considering that BSTs can not contain duplicate Nodes.
SOLUTION
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
class Solution {
// Define the range of valid values for the binary tree nodes
private static final int MIN = 1;
private static final int MAX = 100000;
boolean isBST(Node root) {
return isBSTHelper(root, MIN, MAX);
}
// 'minAllowed' and 'maxAllowed' define the valid range for the current node's value
boolean isBSTHelper(Node root, int minAllowed, int maxAllowed) {
// Base case: if the current node is null, it's a valid BST (empty tree is considered valid)
if (root == null)
return true;
// Check if the current node's value violates the BST property
// It must be within the allowed range [minAllowed, maxAllowed]
if (root.data < minAllowed || root.data > maxAllowed) {
return false; // If out of bounds, it's not a valid BST
}
// Recursively check the left subtree, updating the max allowed value to root.data - 1
// Recursively check the right subtree, updating the min allowed value to root.data + 1
return isBSTHelper(root.left, minAllowed, root.data - 1) &&
isBSTHelper(root.right, root.data + 1, maxAllowed);
}
}