Skip to content

Commit 0889df9

Browse files
committed
Problem 174: Validate if a binary tree is binary search tree
1 parent 180b195 commit 0889df9

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 173 |
9+
| Total Problems | 174 |
1010

1111
</center>
1212

@@ -129,6 +129,7 @@ Include contains single header implementation of data structures and some algori
129129
| Invert a binary tree, recursively and iteratively.| [invert_a_tree.cpp](tree_problems/invert_a_tree.cpp) |
130130
| Given a Binary Search Tree, find ceil and floor of a given key in it. If the given key lie in the BST, then both floor and ceil is equal to that key, else ceil is equal to next greater key (if any) in the BST and floor is equal to previous greater key (if any) in the BST | [floor_ceil_bst.cpp](tree_problems/floor_ceil_bst.cpp) |
131131
| Find kth smallest element in a binary search tree | [kth_smallest.cpp](tree_problems/kth_smallest.cpp)|
132+
| Validate if a given binary tree is a binary search tree. | [validate_bst.cpp](tree_problems/validate_bst.cpp) |
132133

133134
### String Problems
134135
| Problem | Solution |

tree_problems/validate_bst.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Validate if a given binary tree is a binary search tree.
3+
*
4+
* Example:
5+
*
6+
* 10
7+
* / \
8+
* 5 15
9+
*
10+
* Answer: True
11+
*
12+
* Example:
13+
*
14+
* 10
15+
* / \
16+
* 5 15
17+
* / \ / \
18+
* 2 7 9 17
19+
*
20+
* Answer : False
21+
*/
22+
23+
#include <iostream>
24+
25+
struct TreeNode {
26+
int data;
27+
TreeNode* left;
28+
TreeNode* right;
29+
TreeNode(int d) :
30+
data{d}, left{nullptr}, right{nullptr} {}
31+
};
32+
33+
bool is_valid_bst(TreeNode* root, TreeNode* min, TreeNode* max)
34+
{
35+
if (root == nullptr) {
36+
return true;
37+
}
38+
39+
if (min != nullptr && root->data <= min->data) {
40+
return false;
41+
}
42+
43+
if (max != nullptr && root->data >= max->data) {
44+
return false;
45+
}
46+
47+
return is_valid_bst(root->left, min, root) && is_valid_bst(root->right, root, max);
48+
49+
}
50+
51+
bool is_valid_bst(TreeNode* root)
52+
{
53+
return is_valid_bst(root, nullptr, nullptr);
54+
}
55+
56+
void print_inorder(TreeNode* root)
57+
{
58+
if (root != nullptr) {
59+
print_inorder(root->left);
60+
std::cout << root->data << " ";
61+
print_inorder(root->right);
62+
}
63+
}
64+
65+
int main()
66+
{
67+
TreeNode* root1 = new TreeNode(10);
68+
root1->left = new TreeNode(5);
69+
root1->right = new TreeNode(15);
70+
std::cout << "Inorder traversal of tree: ";
71+
print_inorder(root1);
72+
std::cout << std::endl;
73+
if (is_valid_bst(root1)) {
74+
std::cout << "Above tree is a valid binary search tree.\n";
75+
} else {
76+
std::cout << "Above tree is not a valid binary search tree.\n";
77+
}
78+
79+
TreeNode* root2 = new TreeNode(10);
80+
root2->left = new TreeNode(5);
81+
root2->right = new TreeNode(15);
82+
root2->left->left = new TreeNode(2);
83+
root2->left->right = new TreeNode(7);
84+
root2->right->left = new TreeNode(9);
85+
root2->right->right = new TreeNode(17);
86+
std::cout << "Inorder traversal of tree: ";
87+
print_inorder(root2);
88+
std::cout << std::endl;
89+
if (is_valid_bst(root2)) {
90+
std::cout << "Above tree is a valid binary search tree.\n";
91+
} else {
92+
std::cout << "Above tree is not a valid binary search tree.\n";
93+
}
94+
return 0;
95+
}

0 commit comments

Comments
 (0)