Skip to content

Commit 659b3cb

Browse files
committed
Add BST Checker problem
1 parent 8d69388 commit 659b3cb

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

BST Checker/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Binary Search Tree Checker
2+
[Try the problem](https://leetcode.com/problems/validate-binary-search-tree/)
3+
4+
Given a binary tree, determine if it is a valid binary search tree (BST).
5+
6+
Assume a BST is defined as follows:
7+
- The left subtree of a node contains only nodes with keys less than the node's key.
8+
- The right subtree of a node contains only nodes with keys greater than the node's key.
9+
- Both the left and right subtrees must also be binary search trees.
10+
11+
### Example 1
12+
13+
```
14+
2
15+
/ \
16+
1 3
17+
18+
Input: [2,1,3]
19+
Output: true
20+
```
21+
22+
### Example 2
23+
24+
```
25+
5
26+
/ \
27+
1 4
28+
/ \
29+
3 6
30+
31+
Input: [5,1,4,null,null,3,6]
32+
Output: false
33+
Explanation: The root node's value is 5 but its right child's value is 4.
34+
```

BST Checker/solution.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
/**
5+
* Definition for a binary tree node.
6+
*/
7+
struct TreeNode {
8+
int val;
9+
TreeNode *left;
10+
TreeNode *right;
11+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
12+
};
13+
14+
TreeNode* insertLeft(TreeNode* root, int value) {
15+
root->left = new TreeNode(value);
16+
return root->left;
17+
}
18+
19+
TreeNode* insertRight(TreeNode* root, int value) {
20+
root->right = new TreeNode(value);
21+
return root->right;
22+
}
23+
24+
class Solution {
25+
public:
26+
bool isValid(TreeNode* root, int minVal, int maxVal, bool noMin, bool noMax) {
27+
if (root == NULL) {
28+
return true;
29+
}
30+
if (!noMin && root->val <= minVal) {
31+
return false;
32+
}
33+
if (!noMax && root->val >= maxVal) {
34+
return false;
35+
}
36+
bool rightValid = false;
37+
bool leftValid = false;
38+
rightValid = isValid(root->right, root->val, maxVal, false, noMax);
39+
leftValid = isValid(root->left, minVal, root->val, noMin, false);
40+
return rightValid && leftValid;
41+
}
42+
bool isValidBST(TreeNode* root) {
43+
return isValid(root, 0, 0, true, true);
44+
}
45+
};
46+
47+
int main() {
48+
auto root1 = new TreeNode(50);
49+
Solution solver;
50+
cout << solver.isValidBST(root1) << endl; // true
51+
52+
auto root2 = new TreeNode(50);
53+
insertLeft(root2, 30);
54+
insertLeft(root2->left, 10);
55+
insertRight(root2, 70);
56+
insertRight(root2->right, 80);
57+
insertRight(root2->left, 40);
58+
insertLeft(root2->right, 60);
59+
cout << solver.isValidBST(root2) << endl; // true
60+
61+
auto root3 = new TreeNode(50);
62+
insertLeft(root3, 30);
63+
insertLeft(root3->left, 20);
64+
insertRight(root3, 80);
65+
insertRight(root3->right, 90);
66+
insertRight(root3->left, 60);
67+
insertLeft(root3->right, 70);
68+
cout << solver.isValidBST(root3) << endl; // false
69+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ This repository contains the coding interview problems along with solutions.
66
| [Island Count](Island%20Count) | | [CPP](Island%20Count/solution.cpp) | Graph Traversal | Medium |
77
| [Minimum Falling Path Sum](Minimum%20Falling%20Path%20Sum) | [text](Minimum%20Falling%20Path%20Sum/solution.txt) | [CPP](Minimum%20Falling%20Path%20Sum/solution.cpp) | Dynamic Programming | Medium |
88
| [Word Break](Word%20Break) | [text](Word%20Break/solution.txt) | [CPP](Word%20Break/solution.cpp) | Dynamic Programming | Medium |
9-
| [Remove K Digits](Remove%20K%20Digits) | [text](Remove%20K%20Digits/solution.txt) | [CPP](Remove%20K%20Digits/solution.cpp) | Greedy, Stack | Medium |
9+
| [Remove K Digits](Remove%20K%20Digits) | [text](Remove%20K%20Digits/solution.txt) | [CPP](Remove%20K%20Digits/solution.cpp) | Greedy, Stack | Medium |
10+
| [Binary Search Tree Checker](BST%20Checker) | To be updated | [CPP](BST%20Checker/solution.cpp) | Trees | Medium |

0 commit comments

Comments
 (0)