-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidateBinarySearchTree.h
56 lines (54 loc) · 1.33 KB
/
ValidateBinarySearchTree.h
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/**
*Solution 1
*dfs
*time:O(n) space:O(logn)
*/
class Solution {
public:
bool isValidBST(TreeNode *root) {
return dfs(root,1<<31,~(1<<31));
}
private:
bool dfs(TreeNode *node,int minVal,int maxVal)
{
if(!node)return true;
if(node->val<=minVal||node->val>=maxVal)return false;
return dfs(node->left,minVal,node->val)&&dfs(node->right,node->val,maxVal);
}
};
/**
*Solution 2
*dfs
*time:O(n) space:O(logn)
*/
class Solution {
public:
bool isValidBST(TreeNode *root) {
if(!root)return true;
if(root->left)
{
if(root->left->val>=root->val)return false;
TreeNode *leftMax=root->left;
while(leftMax->right)leftMax=leftMax->right;
if(leftMax->val>=root->val)return false;
}
if(root->right)
{
if(root->right->val<=root->val)return false;
TreeNode *rightMin=root->right;
while(rightMin->left)rightMin=rightMin->left;
if(rightMin->val<=root->val)return false;
}
if(!isValidBST(root->left)||!isValidBST(root->right))return false;
return true;
}
};