{"id":943,"date":"2025-12-12T19:06:54","date_gmt":"2025-12-12T11:06:54","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=943"},"modified":"2025-12-12T19:06:54","modified_gmt":"2025-12-12T11:06:54","slug":"leetcode-98-%e9%aa%8c%e8%af%81%e4%ba%8c%e5%8f%89%e6%90%9c%e7%b4%a2%e6%a0%91","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=943","title":{"rendered":"LeetCode 98 \u2014 \u9a8c\u8bc1\u4e8c\u53c9\u641c\u7d22\u6811"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \uff0c\u5224\u65ad\u5176\u662f\u5426\u662f\u4e00\u4e2a\u6709\u6548\u7684\u4e8c\u53c9\u641c\u7d22\u6811\u3002<\/p>\n<p>\u6709\u6548 \u4e8c\u53c9\u641c\u7d22\u6811\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<p>\u8282\u70b9\u7684\u5de6\u5b50\u6811\u53ea\u5305\u542b \u4e25\u683c\u5c0f\u4e8e \u5f53\u524d\u8282\u70b9\u7684\u6570\u3002<br \/>\n\u8282\u70b9\u7684\u53f3\u5b50\u6811\u53ea\u5305\u542b \u4e25\u683c\u5927\u4e8e \u5f53\u524d\u8282\u70b9\u7684\u6570\u3002<br \/>\n\u6240\u6709\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u81ea\u8eab\u5fc5\u987b\u4e5f\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u3002<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_304260b4c5a8bfcaf2322461e0a3fa51.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_304260b4c5a8bfcaf2322461e0a3fa51.jpg\" alt=\"\" \/><\/a><\/p>\n<p>\u8f93\u5165\uff1aroot = [5,1,4,null,null,3,6]<br \/>\n\u8f93\u51fa\uff1afalse<br \/>\n\u89e3\u91ca\uff1a\u6839\u8282\u70b9\u7684\u503c\u662f 5 \uff0c\u4f46\u662f\u53f3\u5b50\u8282\u70b9\u7684\u503c\u662f 4 \u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<h2>\u9012\u5f52<\/h2>\n<p>\u5bf9\u4e8e\u4e8c\u53c9\u641c\u7d22\u6811 t \u6765\u8bf4\uff1a<br \/>\n&#8211; \u5de6\u5b50\u6811\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u4e14\u6240\u6709\u7ed3\u70b9\u503c\u5c0f\u4e8e t<br \/>\n&#8211; \u53f3\u5b50\u6811\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u4e14\u6240\u6709\u7ed3\u70b9\u503c\u5927\u4e8e t<\/p>\n<p>\u56e0\u6b64\u53ef\u4ee5\u9012\u5f52\u5224\u65ad\uff0c\u6838\u5fc3\u662f\u6bcf\u6b21\u90fd\u628a\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u7684\u9650\u5236\u4f20\u9012\u4e0b\u53bb<\/p>\n<h2>\u4e2d\u5e8f\u904d\u5386<\/h2>\n<p>\u4f7f\u7528\u4e2d\u5e8f\u904d\u5386\uff0c\u628a\u4e8c\u53c9\u6811\u8f6c\u6210\u6570\u7ec4\uff0c\u518d\u5224\u65ad\u6570\u7ec4\u662f\u5426\u6709\u5e8f\u5373\u53ef<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public boolean isValidBST(TreeNode root) {\n    return isValidBST(root, null, null);\n}\n\npublic boolean isValidBST(TreeNode root, Integer minValue, Integer maxValue) {\n    if (root == null) {\n        return true;\n    }\n    int value = root.val;\n    if ((minValue != null &amp;&amp; value &lt;= minValue) || (maxValue != null &amp;&amp; value &gt;= maxValue)) {\n        return false;\n    }\n\n    return isValidBST(root.left, minValue, value) &amp;&amp; isValidBST(root.right, value, maxValue);\n}\n\npublic boolean isValidBST2(TreeNode root) {\n    List&lt;Integer&gt; list = dfs(root);\n    for (int i = 0; i &lt; list.size(); i++) {\n        if (i &gt;= 1 &amp;&amp; list.get(i) &lt;= list.get(i - 1)) {\n            return false;\n        }\n    }\n    return true;\n}\n\nprivate List&lt;Integer&gt; dfs(TreeNode root) {\n    List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n    if (root == null) {\n        return list;\n    }\n    list.addAll(dfs(root.left));\n    list.add(root.val);\n    list.addAll(dfs(root.right));\n    return list;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun isValidBST(root: TreeNode?, minValue: Int? = null, maxValue: Int? = null): Boolean {\n    if (root == null) {\n        return true\n    }\n    val value = root.`val`\n    if ((minValue != null &amp;&amp; value &lt;= minValue) || (maxValue != null &amp;&amp; value &gt;= maxValue)) {\n        return false\n    }\n\n    return isValidBST(root.left, minValue, value) &amp;&amp; isValidBST(root.right, value, maxValue)\n}\n\n\nfun isValidBST(root: TreeNode?): Boolean {\n    val list = dfs(root)\n    for (i in list.indices) {\n        if (i &gt;= 1 &amp;&amp; list[i] &lt;= list[i - 1]) {\n            return false\n        }\n    }\n    return true\n}\n\nprivate fun dfs(root: TreeNode?): List&lt;Int&gt; {\n    val ans = ArrayList&lt;Int&gt;()\n    if (root == null) {\n        return ans\n    }\n    ans.addAll(dfs(root.left))\n    ans.add(root.`val`)\n    ans.addAll(dfs(root.right))\n    return ans\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \uff0c\u5224\u65ad\u5176\u662f\u5426\u662f\u4e00\u4e2a\u6709\u6548\u7684\u4e8c\u53c9\u641c\u7d22\u6811\u3002 \u6709\u6548 \u4e8c\u53c9\u641c\u7d22\u6811\u5b9a\u4e49\u5982\u4e0b\uff1a  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[11,20,33,64],"class_list":["post-943","post","type-post","status-publish","format-standard","hentry","category-algo","tag-dfs","tag-leetcode","tag-33","tag-64"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=943"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/943\/revisions"}],"predecessor-version":[{"id":944,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/943\/revisions\/944"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}