-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-98
More file actions
43 lines (43 loc) · 870 Bytes
/
LeetCode-98
File metadata and controls
43 lines (43 loc) · 870 Bytes
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
/*
judge if is a valid BST
inorder traversal the tree, if it is increase.
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(root == NULL)
{
return true;
}
vector<int> v;
visit(root, v);
int i;
for(i = 0; i < (v.size() - 1); ++i)
{
if(v[i] >= v[i + 1])
{
return false;
}
}
return true;
}
void visit(TreeNode *root, vector<int> &v)
{
if(root == NULL)
{
return;
}
visit(root->left, v);
v.push_back(root->val);
visit(root->right, v);
}
};