Skip to content

Instantly share code, notes, and snippets.

Created November 15, 2017 17:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
isValidBST created by Hexe -
#include <iostream>
#include <climits>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(nullptr), right(nullptr) {}
class ValidateBST {
bool isValidBST(TreeNode* root) {
return isValidBST(root, INT_MIN, INT_MAX);
bool isValidBST(TreeNode* node, int lower, int upper) {
if (node == nullptr) return true;
int val = node->val;
return val >= lower && val <= upper
&& isValidBST(node->left, lower, val)
&& isValidBST(node->right, val, upper);
using namespace std;
int main() {
auto root = new TreeNode(5);
root->left = new TreeNode(3);
root->right = new TreeNode(9);
root->right->right = new TreeNode(10);
ValidateBST solution = ValidateBST();
cout << solution.isValidBST(root);
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment