-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_98_ValidateBST.cpp
89 lines (80 loc) · 2.46 KB
/
LC_98_ValidateBST.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
https://leetcode.com/problems/validate-binary-search-tree/
98. Validate Binary Search Tree
*/
/**
* 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) {}
* };
*/
/**
* 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:
// ITERATIVE APPROACH INORDER
bool isValidBST(TreeNode* root) {
TreeNode* prev = nullptr;
stack<TreeNode*> predecessors;
while(root || !predecessors.empty())
{
while(root)
{
predecessors.push(root);
root=root->left;
}
root = predecessors.top(); predecessors.pop();
if(prev && prev->val >= root->val) return false;
prev = root;
root = root->right;
}// while predecssor
return true;
}// end
// min and max are bound of the node value it can take as we enter into tree
/*
bool checkValidity(TreeNode* t, int min, int max)
{
if(!t) return true;
if(t->val <= min || t->val >= max)
return false;
return checkValidity(t->left, min, t->val) && checkValidity(t->right, t->val, max);
}
bool isValidBST(TreeNode* root) {
if(!root) return true;
return checkValidity(root, INT_MIN, INT_MAX);
}// end
*/
/****** USING EXTRA SPACE OF VECTOR ********/
// void inorder(TreeNode* t, vector<int>&v)
// {
// if(!t)
// return;
// inorder(t->left, v);
// v.push_back(t->val);
// inorder(t->right, v);
// }
// bool isValidBST(TreeNode* root) {
// if(!root) return true;
// vector<int> v;
// inorder(root,v);
// for(int i = 1; i<v.size(); i++)
// {
// if(v[i] <= v[i-1]) return false;
// }
// return true;
// }// end
};