diff options
Diffstat (limited to 'other/validate_bst.cpp')
-rw-r--r-- | other/validate_bst.cpp | 24 |
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); + } +}; |