Skip to content

Commit 2b82aa0

Browse files
authored
Merge pull request #1708 from akgmage/dev
add validate bst in c++
2 parents 8541441 + 77a654d commit 2b82aa0

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

Graphs/validate_bst.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Write a function that takes in a potentially invalid Binary Search Tree (BST) and returns a boolean representing
3+
whether the BST is valid.
4+
5+
Explanation:
6+
7+
This code defines a Binary Search Tree (BST) struct with an integer value and left and right nodes that can point to other
8+
BST nodes. The struct also has a method called ValidateBst() that returns a boolean indicating whether the tree is a valid
9+
BST or not.
10+
11+
The BST struct has another method called validateBST() that is used by ValidateBst() to check whether the tree is a valid
12+
BST or not. The validateBST() method takes in two arguments, min and max, which represent the minimum and maximum values
13+
that the current node's value can take in order to be a valid BST.
14+
15+
The validateBST() method first checks whether the current node's value is within the valid range determined by the min and
16+
max arguments. If not, the method returns false, indicating that the tree is not a valid BST.
17+
18+
If the current node's value is within the valid range, the method then recursively calls itself on the left and right
19+
child nodes to check whether their values are within their valid ranges. The valid range for the left child node is defined by the minimum value and the parent node's value, while the valid range for the right child node is defined by the parent node's value and the maximum value.
20+
21+
If all of the nodes in the tree satisfy the BST property, the method returns true, indicating that the tree is a valid BST.
22+
23+
O(n) time | O(d) space - where n is the number of nodes in the BST and d is the depth (height) of the BST
24+
*/
25+
#include <iostream>
26+
#include <limits>
27+
28+
struct TreeNode {
29+
int value;
30+
TreeNode* left;
31+
TreeNode* right;
32+
33+
TreeNode(int x) : value(x), left(nullptr), right(nullptr) {}
34+
};
35+
36+
// Function to check if the BST is valid
37+
bool isValidBST(TreeNode* root) {
38+
return isValidBSTHelper(root, std::numeric_limits<long long>::min(), std::numeric_limits<long long>::max());
39+
}
40+
41+
// Recursive helper function to check if the BST is valid
42+
bool isValidBSTHelper(TreeNode* node, long long minVal, long long maxVal) {
43+
// Base case: if the current node's value is outside the allowed range, then the tree is invalid
44+
if (!node || node->value <= minVal || node->value >= maxVal) {
45+
return false;
46+
}
47+
48+
// Recursively check the left subtree, making sure all values are less than the current node's value
49+
if (!isValidBSTHelper(node->left, minVal, node->value)) {
50+
return false;
51+
}
52+
53+
// Recursively check the right subtree, making sure all values are greater than or equal to the current node's value
54+
if (!isValidBSTHelper(node->right, node->value, maxVal)) {
55+
return false;
56+
}
57+
58+
// If we reach this point, then the tree is valid
59+
return true;
60+
}
61+
62+
int main() {
63+
TreeNode* root = new TreeNode(10);
64+
root->left = new TreeNode(5);
65+
root->right = new TreeNode(15);
66+
root->left->left = new TreeNode(2);
67+
root->left->right = new TreeNode(7);
68+
69+
if (isValidBST(root)) {
70+
std::cout << "The BST is valid." << std::endl;
71+
} else {
72+
std::cout << "The BST is not valid." << std::endl;
73+
}
74+
75+
// Clean up memory
76+
delete root->left->left;
77+
delete root->left->right;
78+
delete root->left;
79+
delete root->right;
80+
delete root;
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)