Validate Binary Search Tree Solutions in C++
Number 98
Difficulty Medium
Acceptance 27.8%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/validate-binary-search-tree/// Author : Hao Chen// Date : 2014-07-05#include <iostream>#include <vector>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};bool isValidBST(TreeNode *root) {//travel the tree by inner-ordervector<TreeNode*> stack;TreeNode* node = root;vector<int> v;while (stack.size()>0 || node!=NULL) {if (node!=NULL){stack.push_back(node);node = node->left;}else{node = stack.back();stack.pop_back();v.push_back(node->val);node = node->right;}}//check the vector wehther sorted or notfor(int i=0; v.size()>0 && i<v.size()-1; i++){if (v[i] >= v[i+1]) {return false;}}return true;}TreeNode* createTree(int a[], int n){if (n<=0) return NULL;TreeNode **tree = new TreeNode*[n];for(int i=0; i<n; i++) {if (a[i]==0 ){tree[i] = NULL;continue;}tree[i] = new TreeNode(a[i]);}int pos=1;for(int i=0; i<n && pos<n; i++) {if (tree[i]){tree[i]->left = tree[pos++];if (pos<n){tree[i]->right = tree[pos++];}}}return tree[0];}int main(){cout << isValidBST(NULL) << endl;int a[]={1,1};cout << isValidBST(createTree(a, sizeof(a)/sizeof(int))) << endl;int b[]={4,2,6,1,7,5,7};cout << isValidBST(createTree(b, sizeof(b)/sizeof(int))) << endl;int c[]={4,2,6,1,3,5,7};cout << isValidBST(createTree(c, sizeof(c)/sizeof(int))) << endl;return 0;}
C++ solution by pezy/LeetCode
#include <cstddef>struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:bool isValidBST(TreeNode *root) {return isValidBST(root, false, false, 0, 0);}bool isValidBST(TreeNode *root, bool left, bool right, int min, int max){if (!root) return true;else if ((left && root->val <= min) || (right && root->val >= max)) return false;else return isValidBST(root->left, left, true, min, root->val) && isValidBST(root->right, true, right, root->val, max);}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/validate-binary-search-tree/description//// Author : liuyubobobo/// Time : 2018-05-09#include <iostream>#include <vector>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Using inOrder traverse/// Store all elements in an vector////// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:bool isValidBST(TreeNode* root) {vector<int> vec;inOrder(root, vec);for(int i = 1 ; i < vec.size() ; i ++)if(vec[i-1] >= vec[i])return false;return true;}private:void inOrder(TreeNode* node, vector<int>& vec){if(node == NULL)return;inOrder(node->left, vec);vec.push_back(node->val);inOrder(node->right, vec);}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/validate-binary-search-tree/description//// Author : liuyubobobo/// Time : 2018-05-09#include <iostream>#include <vector>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Recursive test/// Time Complexity: O(n)/// Space Complexity: O(h)class Solution {public:bool isValidBST(TreeNode* root) {return isValidBST(root, INT_MIN, INT_MAX);}private:bool isValidBST(TreeNode* node, int min, int max){if(node == NULL)return true;if(node->val < min || node->val > max)return false;if(node->left != NULL && node->left->val >= node->val)return false;if(node->right != NULL && node->right->val <= node->val)return false;return isValidBST(node->left, min, node->val - 1) && isValidBST(node->right, node->val + 1, max);}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/validate-binary-search-tree/description//// Author : liuyubobobo/// Time : 2018-12-16#include <iostream>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Non-Recursion Solution for Recursion thinking/// Time Complexity: O(n)/// Space Complexity: O(h)class Solution {public:bool isValidBST(TreeNode* root) {if(!root) return true;stack<TreeNode*> st;stack<int> upper, lower;st.push(root);upper.push(INT_MAX);lower.push(INT_MIN);while(!st.empty()){TreeNode* cur = st.top();st.pop();int left = lower.top();lower.pop();int right = upper.top();upper.pop();if(cur->val > right || cur->val < left)return false;if(cur->right){if(cur->right->val <= cur->val) return false;st.push(cur->right);lower.push(cur->val + 1);upper.push(right);}if(cur->left){if(cur->left->val >= cur->val) return false;st.push(cur->left);lower.push(left);upper.push(cur->val - 1);}}return true;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/validate-binary-search-tree/description//// Author : liuyubobobo/// Time : 2018-05-22#include <iostream>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// InOrder traverse/// validate during the traversing////// Time Complexity: O(n)/// Space Complexity: O(h)class Solution {private:int pre = -1;bool isPre = false;public:bool isValidBST(TreeNode* root) {isPre = false;pre = -1;return inOrder(root);}private:bool inOrder(TreeNode* node){if(node == NULL)return true;if(!inOrder(node->left))return false;if(isPre && pre >= node->val)return false;isPre = true;pre = node->val;if(!inOrder(node->right))return false;return true;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/validate-binary-search-tree/description//// Author : liuyubobobo/// Time : 2018-05-28#include <iostream>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Morris InOrder traversal/// Attention: you can not change the give Tree structure in Leetcode,/// So try to return result early will lead to RE :-)////// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {private:bool flag;int pre;public:bool isValidBST(TreeNode* root) {flag = false;pre = -1;TreeNode* cur = root;bool res = true;while(cur != NULL){if(cur->left == NULL){if(!process(cur))res = false;cur = cur->right;}else{TreeNode* prev = cur->left;while(prev->right != NULL && prev->right != cur)prev = prev->right;if(prev->right == NULL){prev->right = cur;cur = cur->left;}else{prev->right = NULL;if(!process(cur))res = false;cur = cur->right;}}}return res;}private:bool process(TreeNode* node){if(flag && pre >= node->val)return false;flag = true;pre = node->val;return true;}};void print_bool(bool res){cout << (res ? "True" : "False") << endl;}int main() {TreeNode* root = new TreeNode(5);root->left = new TreeNode(1);root->right = new TreeNode(4);root->right->left = new TreeNode(3);root->right->right = new TreeNode(6);print_bool(Solution().isValidBST(root));return 0;}