{"id":935,"date":"2025-12-12T15:48:43","date_gmt":"2025-12-12T07:48:43","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=935"},"modified":"2025-12-12T15:48:43","modified_gmt":"2025-12-12T07:48:43","slug":"leetcode-101-%e5%af%b9%e7%a7%b0%e4%ba%8c%e5%8f%89%e6%a0%91","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=935","title":{"rendered":"LeetCode 101 \u2014 \u5bf9\u79f0\u4e8c\u53c9\u6811"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \uff0c \u68c0\u67e5\u5b83\u662f\u5426\u8f74\u5bf9\u79f0\u3002<\/p>\n<p>\u793a\u4f8b1<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_02f249aaa4cbe60ef015ff6430cfe0ff.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_02f249aaa4cbe60ef015ff6430cfe0ff.jpg\" alt=\"\" \/><\/a><br \/>\n\u8f93\u5165\uff1aroot = [1,2,2,3,4,4,3]<br \/>\n\u8f93\u51fa\uff1atrue<\/p>\n<p>\u793a\u4f8b2<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_031dbb7581f988db0f5a4d9164d65af4.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/12\/wp_editor_md_031dbb7581f988db0f5a4d9164d65af4.jpg\" alt=\"\" \/><\/a><br \/>\n\u8f93\u5165\uff1aroot = [1,2,2,null,3,null,3]<br \/>\n\u8f93\u51fa\uff1afalse<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<h2>\u9012\u5f52<\/h2>\n<p>\u4e8c\u53c9\u6811\u5bf9\u79f0 \u7b49\u4ef7\u4e8e \u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5bf9\u79f0\uff0c\u56e0\u6b64\u65b0\u589e\u4e00\u4e2a\u65b9\u6cd5\u5224\u65ad\u4e24\u4e2a\u6811 t1 \u548c t2 \u662f\u5426\u76f8\u4e92\u5bf9\u79f0\u3002<br \/>\nt1 \u548c t2 \u5bf9\u79f0 = \uff08\u4e24\u4e2a\u6839\u8282\u70b9\u503c\u76f8\u7b49\uff09\u4e14\uff08t1.left \u548c t2.right \u5bf9\u79f0\uff09\u4e14\uff08t1.right \u548c t2.left \u5bf9\u79f0\uff09<\/p>\n<h2>\u8fed\u4ee3<\/h2>\n<p>\u4f7f\u7528\u961f\u5217\u8fdb\u884c\u5c42\u5e8f\u904d\u5386\uff0c\u5224\u65ad\u6bcf\u4e00\u5c42\u7684\u6570\u636e\u662f\u5426\u5bf9\u79f0<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">\/\/ \u9012\u5f52\n\/\/ \u5c06\u95ee\u9898\u8f6c\u5316\u4e3a \u5de6\u5b50\u6811 t1 \u548c\u53f3\u5b50\u6811 t2 \u5bf9\u79f0\n\/\/ \u4e24\u4e2a\u6839\u7684\u503c\u76f8\u540c\uff0c\u4e14 t1.left \u548c t2.right \u5bf9\u79f0\uff0ct1.right \u548c t2.left \u5bf9\u79f0\npublic boolean isSymmetric(TreeNode root) {\n    return isSymmetric(root.left, root.right);\n}\n\nprivate boolean isSymmetric(TreeNode t1, TreeNode t2) {\n    if (t1 == null &amp;&amp; t2 == null) {\n        return true;\n    }\n    if (t1 == null || t2 == null) {\n        return false;\n    }\n    return t1.val == t2.val &amp;&amp; isSymmetric(t1.left, t2.right) &amp;&amp; isSymmetric(t1.right, t2.left);\n}\n\n\n\/\/ \u8fed\u4ee3\n\/\/ \u5c42\u5e8f\u904d\u5386\n\/\/ \u5224\u8bfb\u6bcf\u4e00\u5c42\u662f\u5426\u5bf9\u79f0\npublic boolean isSymmetric2(TreeNode root) {\n    Queue&lt;TreeNode&gt; queue = new LinkedList&lt;&gt;();\n    queue.offer(root);\n    while (!queue.isEmpty()) {\n        int size = queue.size();\n        \/\/ \u4e00\u5c42\u5165\u961f\n        while (size &gt; 0) {\n            TreeNode node = queue.poll();\n            if (node != null) {\n                queue.offer(node.left);\n                queue.offer(node.right);\n            }\n            size--;\n        }\n        \/\/ \u68c0\u67e5\u4e00\u5c42\u662f\u5426\u5bf9\u79f0\n        LinkedList&lt;TreeNode&gt; list = new LinkedList&lt;&gt;(queue);\n        while (!list.isEmpty()) {\n            TreeNode first = list.pollFirst();\n            TreeNode last = list.pollLast();\n            Integer firstVal = first == null ? Integer.MIN_VALUE : first.val;\n            Integer lastVal = last == null ? Integer.MIN_VALUE : last.val;\n            if (!firstVal.equals(lastVal)) {\n                return false;\n            }\n        }\n    }\n    return true;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun isSymmetric(root: TreeNode?): Boolean {\n    return isSymmetric(root?.left, root?.right)\n}\n\nprivate fun isSymmetric(t1: TreeNode?, t2: TreeNode?): Boolean {\n    if (t1 == null &amp;&amp; t2 == null) {\n        return true\n    }\n    if (t1 == null || t2 == null) {\n        return false\n    }\n    return t1.`val` == t2.`val` &amp;&amp; isSymmetric(t1.left, t2.right) &amp;&amp; isSymmetric(t1.right, t2.left)\n}\n\nfun isSymmetric2(root: TreeNode?): Boolean {\n    val queue = LinkedList&lt;TreeNode?&gt;()\n    queue.offer(root)\n    while (!queue.isEmpty()) {\n        var size = queue.size\n        while (size &gt; 0) {\n            val node = queue.poll()\n            if (node != null) {\n                queue.offer(node.left)\n                queue.offer(node.right)\n            }\n            size--\n        }\n        val list = LinkedList(queue)\n        while (!list.isEmpty()) {\n            val first = list.pollFirst()\n            val last = list.pollLast()\n            val firstVal = first?.`val` ?: Int.MAX_VALUE\n            val lastVal = last?.`val` ?: Int.MAX_VALUE\n            if (firstVal != lastVal) {\n                return false\n            }\n        }\n    }\n\n    return true\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 \u68c0\u67e5\u5b83\u662f\u5426\u8f74\u5bf9\u79f0\u3002 \u793a\u4f8b1 \u8f93\u5165\uff1aroot = [1,2,2, [&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":[20,33,64,87],"class_list":["post-935","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-33","tag-64","tag-87"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/935","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=935"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/935\/revisions"}],"predecessor-version":[{"id":936,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/935\/revisions\/936"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}