# 验证二叉搜索树

<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://img-service.csdnimg.cn/img_convert/3d85b39145ecc61874276bcb7cfe6765.jpeg" alt="">
    <pre><strong>输入：</strong>root = [2,1,3]
<strong>输出：</strong>true
</pre>

    <p><strong>示例 2：</strong></p>
    <img style="width: 422px; height: 292px;" src="https://img-service.csdnimg.cn/img_convert/f8c9b63a549daf28d19243f8141bb56d.jpeg" alt="">
    <pre><strong>输入：</strong>root = [5,1,4,null,null,3,6]
<strong>输出：</strong>false
<strong>解释：</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>

以下程序实现了这一功能，请你填补空白处内容：

```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) {}
};
class Solution
{
public:
	bool isValidBST(TreeNode *root)
	{
		stack<TreeNode *> stk;
		int prev = INT_MIN;
		bool first = true;
		while (!stk.empty() || root != nullptr)
		{
			if (root != nullptr)
			{
				stk.push(root);
				root = root->left;
			}
			else
			{
				root = stk.top();
				stk.pop();
				_______________________;
			}
		}
		return true;
	}
};
```

## template

```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) {}
};
class Solution
{
public:
	bool isValidBST(TreeNode *root)
	{
		stack<TreeNode *> stk;
		int prev = INT_MIN;
		bool first = true;
		while (!stk.empty() || root != nullptr)
		{
			if (root != nullptr)
			{
				stk.push(root);
				root = root->left;
			}
			else
			{
				root = stk.top();
				stk.pop();
				if (!first && prev >= root->val)
				{
					return false;
				}
				first = false;
				prev = root->val;
				root = root->right;
			}
		}
		return true;
	}
};
```

## 答案

```cpp
if (!first && prev >= root->val)
{
	return false;
}
first = false;
prev = root->val;
root = root->right;
```

## 选项

### A

```cpp
if (first && prev >= root->val)
{
	return false;
}
first = false;
prev = root->val;
root = root->right;
```

### B

```cpp
if (first || prev >= root->val)
{
	return false;
}
first = false;
prev = root->val;
root = root->right;
```

### C

```cpp
if (!first || prev >= root->val)
{
	return false;
}
first = false;
prev = root->val;
root = root->right;
```