0%

98.验证二叉搜索树

1. 中序遍历判断是否是递增序列

中序遍历:左根右
可以用递归实现,也可以用栈模拟递归栈来迭代实现,这里用迭代实现。
时间复杂度:遍历所有的节点,O(n)
空间复杂度:递归栈,深度最大O(n)

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:
bool isValidBST(TreeNode* root) {
int pre = INT32_MIN;
bool modified = false;
stack<TreeNode*> st;

while (root != nullptr || !st.empty()) {
while (root != nullptr) {
st.push(root);
root = root->left;
}
root = st.top();
st.pop();
if (!modified) {
pre = root->val;
modified = true;
} else {
if (pre < root->val)
pre = root->val;
else
return false;
}
root = root->right;
}

return true;
}
};