-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path98.validate-binary-search-tree.cpp
54 lines (45 loc) · 1.18 KB
/
98.validate-binary-search-tree.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* @lc app=leetcode id=98 lang=cpp
*
* [98] Validate Binary Search Tree
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
void treeToList(TreeNode* root, vector<int>& list) {
if (root->left != nullptr) treeToList(root->left, list);
list.push_back(root->val);
if (root->right != nullptr) treeToList(root->right, list);
}
bool isValidBST(TreeNode* root) {
if (root == nullptr) return true;
vector<int> list;
treeToList(root, list);
for (int i = 0; i < list.size() - 1; i++) {
if (list[i] >= list[i + 1]) {
return false;
}
}
return true;
}
};
// @lc code=end