Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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 isValidBST(TreeNode* root) {
- if(!root) return true;
- return isValid(root, LONG_LONG_MIN, LONG_LONG_MAX);
- }
- bool isValid(TreeNode *root, long long min, long long max){
- if(!root) return true;
- if(root->val <= min || root->val >= max) return false;
- return (isValid(root->left, min, root->val) && isValid(root->right, root->val, max));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment