-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path98.验证二叉搜索树.cpp
39 lines (36 loc) · 1.1 KB
/
98.验证二叉搜索树.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
/*
* @lc app=leetcode.cn id=98 lang=cpp
*
* [98] 验证二叉搜索树
*/
// @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) {}
* };
*/
class Solution {
public:
TreeNode* preNode = nullptr;
// 使用中序遍历
// 记录上一个遍历的节点
// 因为中序遍历二叉搜索树结果是递增的,因此如果上一个节点的值比下一个节点的值大,就说明有错误
// 二叉搜索树里面不存在相同值的节点
bool isValidBST(TreeNode* root) {
if(root == nullptr)
return true;
bool left = isValidBST(root->left);
if(preNode != nullptr && preNode->val >= root->val)
return false;
preNode = root;
bool right = isValidBST(root->right);
return left && right;
}
};
// @lc code=end