solution.md 3.7 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
# 验证二叉搜索树
<div class="notranslate">
    <p>给你一个二叉树的根节点 <code>root</code> ,判断其是否是一个有效的二叉搜索树。</p>

    <p><strong>有效</strong> 二叉搜索树定义如下:</p>

    <ul>
        <li>节点的左子树只包含<strong> 小于 </strong>当前节点的数。</li>
        <li>节点的右子树只包含 <strong>大于</strong> 当前节点的数。</li>
        <li>所有左子树和右子树自身必须也是二叉搜索树。</li>
    </ul>

    <p>&nbsp;</p>

    <p><strong>示例 1:</strong></p>
    <img style="width: 302px; height: 182px;" src="https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg" alt="">
    <pre><strong>输入:</strong>root = [2,1,3]
<strong><br />输出:</strong>true
</pre>

    <p><strong>示例 2:</strong></p>
    <img style="width: 422px; height: 292px;" src="https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg" alt="">
    <pre><strong>输入:</strong>root = [5,1,4,null,null,3,6]
<strong><br />输出:</strong>false
<strong><br />解释:</strong>根节点的值是 5 ,但是右子节点的值是 4 。
</pre>

    <p>&nbsp;</p>

    <p><strong>提示:</strong></p>

    <ul>
        <li>树中节点数目范围在<code>[1, 10<sup>4</sup>]</code> 内</li>
        <li><code>-2<sup>31</sup> &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>
    </ul>
</div>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;

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) {}
};
```
### after
```cpp

```

## 答案
```cpp
class Solution
{
public:
    bool judge(TreeNode *root, long start, long end)
    {
        if (!root)
            return true;
        if (start >= root->val || end <= root->val)
            return false;
        return judge(root->left, start, root->val);
    }

    bool isValidBST(TreeNode *root)
    {
        long start = INT_MIN - 1;
        long end = INT_MAX + 1;
        return judge(root, start, end);
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    TreeNode *pre;
    bool isValidBST(TreeNode *root)
    {
        if (!root)
            return true;
        if (!isValidBST(root->left))
            return false;
        if (pre && pre->val >= root->val)
            return false;
        pre = root;
        if (!isValidBST(root->right))
            return false;
        return true;
    }
};
```

### B
```cpp
class Solution
{
public:
    void midorder(TreeNode *root, vector<int> &arr)
    {
        if (root)
        {
            midorder(root->left, arr);
            arr.push_back(root->val);
            midorder(root->right, arr);
        }
    }
    bool isValidBST(TreeNode *root)
    {
        vector<int> arr;
        midorder(root, arr);
        for (int i = 1; i < arr.size(); i++)
        {
            if (arr[i] <= arr[i - 1])
                return false;
        }
        return true;
    }
};
```

### C
```cpp
class Solution
{
public:
    bool isValidBST(TreeNode *root)
    {
        if (root)
        {
            long long f = -0x3f3f3f3f3f3f;
            return dfs(root, f);
        }
        else
            return true;
    }

    bool dfs(TreeNode *root, long long &f_val)
    {
        bool res = true;
        if (root)
        {
            res &= dfs(root->left, f_val) && (root->val > f_val);
            if (!res)
                return false;
            f_val = root->val;
            res &= dfs(root->right, f_val);
        }
        return res;
    }
};
```