{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "2733703c7dd4468e999179f0bfa8be0b",
  "keywords": "树,深度优先搜索,二叉搜索树,二叉树",
  "title": "验证二叉搜索树",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>给你一个二叉树的根节点 <code>root</code> ，判断其是否是一个有效的二叉搜索树。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>有效</strong> 二叉搜索树定义如下：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li>节点的左子树只包含<strong> 小于 </strong>当前节点的数。</li>\n<li>节点的右子树只包含 <strong>大于</strong> 当前节点的数。</li>\n<li>所有左子树和右子树自身必须也是二叉搜索树。</li>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 1：</strong></p>\n<img style=\"width: 302px; height: 182px;\" src=\"https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg\" alt=\"\">\n<pre><strong>输入：</strong>root = [2,1,3]\n<strong>输出：</strong>true\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2：</strong></p>\n<img style=\"width: 422px; height: 292px;\" src=\"https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg\" alt=\"\">\n<pre><strong>输入：</strong>root = [5,1,4,null,null,3,6]\n<strong>输出：</strong>false\n<strong>解释：</strong>根节点的值是 5 ，但是右子节点的值是 4 。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li>树中节点数目范围在<code>[1, 10<sup>4</sup>]</code> 内</li>\n<li><code>-2<sup>31</sup> &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "import sys\nclass TreeNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\nclass List2Tree(object):\n\tdef __init__(self, nums: list):\n\t\tself.nums = nums\n\t\tself.queue = []\n\t\tif len(nums) == 1:\n\t\t\tself.root = TreeNode(self.nums.pop(0))\n\t\telse:\n\t\t\ta = self.nums.pop(0)\n\t\t\tb = self.nums.pop(0)\n\t\t\tc = self.nums.pop(0)\n\t\t\tself.root = TreeNode(a)\n\t\t\tif b is not None:\n\t\t\t\tself.root.left = TreeNode(b)\n\t\t\telse:\n\t\t\t\tself.root.left = b\n\t\t\tif c is not None:\n\t\t\t\tself.root.right = TreeNode(c)\n\t\t\telse:\n\t\t\t\tself.root.right = c\n\t\t\tself.queue.append(self.root.left)\n\t\t\tself.queue.append(self.root.right)\n\tdef convert(self):\n\t\twhile len(self.nums) > 0 and len(self.queue)> 0:\n\t\t\tnode = self.queue.pop(0)\n\t\t\tif node is not None:\n\t\t\t\tnum= self.nums.pop(0)\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.left = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.left = num\n\t\t\t\tif len(self.nums) > 0:\n\t\t\t\t\tnum = self.nums.pop(0)\n\t\t\t\telse:\n\t\t\t\t\tnum = None\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.right = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.right = num\n\t\t\t\tself.queue.append(node.left)\n\t\t\t\tself.queue.append(node.right)\n\t\treturn self.root\nclass Solution(object):\n\tdef isValidBST(self, root):\n\t\troot = List2Tree(root).convert()\n\t\treturn self.isVaild_helper(root, -sys.maxsize - 1, sys.maxsize)\n\tdef isVaild_helper(self, root, minVal, maxVal):\n\t\tif root is None:\n\t\t\treturn True\n\t\tif root.val >= maxVal or root.val <= minVal:\n\t\t\treturn False\n\t\treturn self.isVaild_helper(root.left, minVal, root.val) and self.isVaild_helper(root.right, root.val, maxVal)\n# %%\ns = Solution()\nprint(s.isValidBST([5,1,4,None,None,3,6]))",
    "language": "python"
  },
  "node_id": "dailycode-27c2c1b4db8748f394d7ec612bf0ac33",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600469827"
}