Skip to content

Commit 67287d2

Browse files
PiyushGoel0612Piyush Goel
andauthored
Added the validate bst function (#2696)
* Added the validate bst function * Added the validate bst function --------- Co-authored-by: Piyush Goel <[email protected]>
1 parent 486fa37 commit 67287d2

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# ===============================================================================
2+
# Validate Binary Search Tree
3+
'''
4+
To check if the given binary tree is a valid binary search tree (BST), we need to ensure that:
5+
1. The left subtree of a node contains only nodes with keys less than the node's key.
6+
2. The right subtree of a node contains only nodes with keys greater than the node's key.
7+
3. Both the left and right subtrees must also be binary search trees.
8+
'''
9+
# ===============================================================================
10+
11+
# Tree class definition
12+
class TreeNode:
13+
14+
def __init__(self, value):
15+
16+
self.val = value
17+
self.left = None
18+
self.right = None
19+
20+
# Function to validate if a binary tree is a BST
21+
def validate_bst(node):
22+
'''
23+
Validate if a binary tree is a binary search tree (BST).
24+
Input params : Tree Node to be validated
25+
Returns : Tuple (
26+
is_bst: bool,
27+
min_value: int | None,
28+
max_value: int | None
29+
)
30+
'''
31+
32+
# Base case: An empty tree is a valid BST
33+
if not node:
34+
return (True, None, None)
35+
36+
# Validate the left and right subtrees
37+
valid_left, minn_left, maxx_left = validate_bst(node.left)
38+
valid_right, minn_right, maxx_right = validate_bst(node.right)
39+
40+
# If either subtree is not valid, the whole tree is not a valid BST
41+
if not valid_left or not valid_right:
42+
return (
43+
False,
44+
minn_left if minn_left else node.val,
45+
maxx_right if maxx_right else node.val
46+
)
47+
48+
# Check the current node's value against the max of the left subtree
49+
if maxx_left is not None and maxx_left > node.val:
50+
return (
51+
False,
52+
minn_left if minn_left else node.val,
53+
maxx_right if maxx_right else node.val
54+
)
55+
56+
# Check the current node's value against the min of the right subtree
57+
if minn_right is not None and minn_right < node.val:
58+
return (
59+
False,
60+
minn_left if minn_left else node.val,
61+
maxx_right if maxx_right else node.val
62+
)
63+
64+
# If all checks pass, the tree/subtree is a valid BST
65+
return (
66+
True,
67+
minn_left if minn_left is not None else node.val,
68+
maxx_right if maxx_right is not None else node.val
69+
)
70+
71+
# Example usage
72+
if __name__ == "__main__":
73+
# Constructing a simple binary tree
74+
root = TreeNode(10)
75+
root.left = TreeNode(5)
76+
root.right = TreeNode(15)
77+
root.right.left = TreeNode(12)
78+
root.right.right = TreeNode(20)
79+
80+
'''
81+
10
82+
/ \
83+
5 15
84+
/ \
85+
12 20
86+
'''
87+
88+
# Validate if the constructed tree is a BST
89+
is_bst, _, _ = validate_bst(root)
90+
print(f"The tree is a valid BST: {is_bst}")

0 commit comments

Comments
 (0)