/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool Less(TreeNode *root, int data) { if(root == NULL) return true; return (root->val < data) && Less(root->left, data) && Less(root->right, data); } bool More(TreeNode *root, int data) { if(root == NULL) return true; return (root->val > data) && More(root->left, data) && More(root->right, data); } bool isValidBST(TreeNode *root) { if(root == NULL) return true; return Less(root->left, root->val) && More(root->right, root->val) && isValidBST(root->left) && isValidBST(root->right); } };