aboutsummaryrefslogtreecommitdiff
path: root/other/validate_bst.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/validate_bst.cpp')
-rw-r--r--other/validate_bst.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/other/validate_bst.cpp b/other/validate_bst.cpp
new file mode 100644
index 0000000..0785d47
--- /dev/null
+++ b/other/validate_bst.cpp
@@ -0,0 +1,24 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ bool isValidBST(TreeNode *root) {
+ if (!root)
+ return true;
+ inOrder(root);
+ for (int i = 1; i < tree.size(); i++)
+ if (tree[i] <= tree[i - 1])
+ return false;
+ return true;
+ }
+
+private:
+ vector<int> tree;
+ void inOrder(TreeNode *root) {
+ if (!root)
+ return;
+ inOrder(root->left);
+ tree.push_back(root->val);
+ inOrder(root->right);
+ }
+};