File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
solution/0098.Validate Binary Search Tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * TreeNode *left;
6
+ * TreeNode *right;
7
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8
+ * };
9
+ */
10
+ class Solution {
11
+ public:
12
+
13
+ void traverse (TreeNode* root, vector<int > &elements){
14
+
15
+ if ( root->left != NULL )
16
+ traverse (root->left , elements);
17
+
18
+ elements.push_back (root->val );
19
+
20
+ if ( root->right != NULL )
21
+ traverse (root->right , elements);
22
+ }
23
+
24
+ bool isValidBST (TreeNode* root) {
25
+
26
+ if ( root == NULL )
27
+ return true ;
28
+
29
+ vector<int > elements;
30
+ traverse (root, elements);
31
+
32
+ for (int i = 0 ; i < elements.size () - 1 ; i++)
33
+ if ( elements[i] >= elements[i + 1 ] )
34
+ return false ;
35
+
36
+ return true ;
37
+ }
38
+ };
You can’t perform that action at this time.
0 commit comments