Skip to content

Commit ecae26b

Browse files
authored
Create main.cpp
1 parent 5f4d9e5 commit ecae26b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isBST(TreeNode* root, long min, long max){
15+
if(root == NULL) return true;
16+
17+
if(root -> val > min && root -> val < max){
18+
bool left = isBST(root -> left, min, root -> val);
19+
bool right = isBST(root -> right, root -> val, max);
20+
21+
return left && right;
22+
}else{
23+
return false;
24+
}
25+
}
26+
bool isValidBST(TreeNode* root) {
27+
return isBST(root, LONG_MIN, LONG_MAX);
28+
}
29+
};

0 commit comments

Comments
 (0)