forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
38 lines (29 loc) · 807 Bytes
/
Solution.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
/**
* 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:
void traverse(TreeNode* root, vector<int> &elements){
if( root->left != NULL )
traverse(root->left, elements);
elements.push_back(root->val);
if( root->right != NULL )
traverse(root->right, elements);
}
bool isValidBST(TreeNode* root) {
if( root == NULL )
return true;
vector<int> elements;
traverse(root, elements);
for(int i = 0 ; i < elements.size() - 1 ; i++)
if( elements[i] >= elements[i + 1] )
return false;
return true;
}
};