-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path98.cpp
More file actions
executable file
·27 lines (27 loc) · 1 KB
/
98.cpp
File metadata and controls
executable file
·27 lines (27 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool checkleft(TreeNode * current,int val){
if (current == NULL) return true;
return (current->val < val) && checkleft(current->left,val) && checkleft(current->right,val);
}
bool checkright(TreeNode * current,int val){
if (current == NULL) return true;
return ((current->val > val) && checkright(current->left,val) && checkright(current->right,val));
}
bool isValidBST(TreeNode* root) {
if (root == NULL)
return true;
return checkleft(root->left,root->val) && checkright(root->right,root->val) && isValidBST(root->left) && isValidBST(root->right);
}
};