97.json 5.8 KB
Newer Older
每日一练社区's avatar
test  
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
{
   "question_id": 98,
   "question_title": "验证二叉搜索树",
   "difficulty": "中等",
   "question_content": "<div class=\"notranslate\">\n    <p>给你一个二叉树的根节点 <code>root</code> ,判断其是否是一个有效的二叉搜索树。</p>\n\n    <p><strong>有效</strong> 二叉搜索树定义如下:</p>\n\n    <ul>\n        <li>节点的左子树只包含<strong> 小于 </strong>当前节点的数。</li>\n        <li>节点的右子树只包含 <strong>大于</strong> 当前节点的数。</li>\n        <li>所有左子树和右子树自身必须也是二叉搜索树。</li>\n    </ul>\n\n    <p>&nbsp;</p>\n\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>\n\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>\n\n    <p>&nbsp;</p>\n\n    <p><strong>提示:</strong></p>\n\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>",
   "topic_link": "https://bbs.csdn.net/topics/600469827",
   "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct TreeNode\n{\n\tint val;\n\tTreeNode *left;\n\tTreeNode *right;\n\tTreeNode() : val(0), left(nullptr), right(nullptr) {}\n\tTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n\tTreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n};\nclass Solution\n{\npublic:\n\tbool isValidBST(TreeNode *root)\n\t{\n\t\tstack<TreeNode *> stk;\n\t\tint prev = INT_MIN;\n\t\tbool first = true;\n\t\twhile (!stk.empty() || root != nullptr)\n\t\t{\n\t\t\tif (root != nullptr)\n\t\t\t{\n\t\t\t\tstk.push(root);\n\t\t\t\troot = root->left;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\troot = stk.top();\n\t\t\t\tstk.pop();\n\t\t\t\tif (!first && prev >= root->val)\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tfirst = false;\n\t\t\t\tprev = root->val;\n\t\t\t\troot = root->right;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n};",
   "java": "\npublic class TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\n\tTreeNode(int x) {\n\t\tval = x;\n\t}\n}\n\nclass Solution {\n\tpublic boolean isValidBST(TreeNode root) {\n\n\t\tif (root == null)\n\t\t\treturn true;\n\t\tif (root.left == null && root.right == null) {\n\t\t\treturn true;\n\t\t}\n\t\tif (root.left != null) {\n\t\t\tTreeNode cur = root.left;\n\t\t\twhile (cur.right != null) {\n\t\t\t\tcur = cur.right;\n\t\t\t}\n\t\t\tif (cur.val >= root.val) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\tif (root.right != null) {\n\t\t\tTreeNode cur = root.right;\n\t\t\twhile (cur.left != null) {\n\t\t\t\tcur = cur.left;\n\t\t\t}\n\t\t\tif (cur.val <= root.val) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\tboolean left = isValidBST(root.left);\n\t\tboolean right = isValidBST(root.right);\n\n\t\treturn left && right;\n\t}\n}",
   "js": "",
   "python": "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]))",
   "status": 1,
   "keywords": "树,深度优先搜索,二叉搜索树,二叉树",
   "license": {
      "cpp": "https://github.com/begeekmyfriend/leetcode",
      "python": "https://github.com/qiyuangong/leetcode",
      "java": "https://github.com/zhangyu345293721/leetcode"
   },
   "notebook": {
      "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/97/97_cpp.ipynb?type=file",
      "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/97/97_python.ipynb?type=file",
      "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/97/97_java.ipynb?type=file"
   },
   "notebook_enable": 1
}