{"id":939,"date":"2025-12-12T16:45:10","date_gmt":"2025-12-12T08:45:10","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=939"},"modified":"2025-12-12T16:45:10","modified_gmt":"2025-12-12T08:45:10","slug":"leetcode-102-%e4%ba%8c%e5%8f%89%e6%a0%91%e7%9a%84%e5%b1%82%e5%ba%8f%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=939","title":{"rendered":"LeetCode 102 \u2014 \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \uff0c\u8fd4\u56de\u5176\u8282\u70b9\u503c\u7684 \u5c42\u5e8f\u904d\u5386 \u3002 \uff08\u5373\u9010\u5c42\u5730\uff0c\u4ece\u5de6\u5230\u53f3\u8bbf\u95ee\u6240\u6709\u8282\u70b9\uff09\u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u4f7f\u7528\u961f\u5217\u8fdb\u884c\u904d\u5386\uff0c\u6838\u5fc3\u662f\u501f\u52a9\u961f\u5217\u7684\u5927\u5c0f\u5f97\u5230\u8fd9\u4e00\u5c42\u8282\u70b9\u7684\u6570\u91cf\uff0c\u6839\u636e\u6570\u91cf\u5b9e\u73b0\u7edf\u4e00\u51fa\u961f\u4ee5\u53ca\u4e0b\u4e00\u5c42\u7684\u7edf\u4e00\u5165\u961f\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public List&lt;List&lt;Integer&gt;&gt; levelOrder(TreeNode root) {\n    List&lt;List&lt;Integer&gt;&gt; ans = new ArrayList&lt;&gt;();\n    if (root == null) {\n        return ans;\n    }\n    Queue&lt;TreeNode&gt; queue = new LinkedList&lt;&gt;();\n    queue.offer(root);\n    while (!queue.isEmpty()) {\n        int size = queue.size();\n        List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n        while (size &gt; 0) {\n            TreeNode node = queue.poll();\n            list.add(node.val);\n            if (node.left != null) {\n                queue.offer(node.left);\n            }\n            if (node.right != null) {\n                queue.offer(node.right);\n            }\n            size--;\n        }\n        if (!list.isEmpty()) {\n            ans.add(list);\n        }\n    }\n    return ans;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun levelOrder(root: TreeNode?): List&lt;List&lt;Int&gt;&gt; {\n    val ans = ArrayList&lt;List&lt;Int&gt;&gt;()\n    if (root == null) {\n        return ans\n    }\n    val queue = LinkedList&lt;TreeNode&gt;()\n    queue.offer(root)\n    while (!queue.isEmpty()) {\n        var size = queue.size\n        val list = ArrayList&lt;Int&gt;()\n        while (size &gt; 0) {\n            val node = queue.poll()\n            list.add(node.`val`)\n            if (node.left != null) {\n                queue.offer(node.left)\n            }\n            if (node.right != null) {\n                queue.offer(node.right)\n            }\n            size--\n        }\n        if (!list.isEmpty()) {\n            ans.add(list)\n        }\n    }\n    return ans\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \uff0c\u8fd4\u56de\u5176\u8282\u70b9\u503c\u7684 \u5c42\u5e8f\u904d\u5386 \u3002 \uff08\u5373\u9010\u5c42\u5730\uff0c\u4ece\u5de6\u5230\u53f3\u8bbf\u95ee\u6240\u6709\u8282\u70b9\uff09\u3002  [&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":[88,20,33,87],"class_list":["post-939","post","type-post","status-publish","format-standard","hentry","category-algo","tag-bfs","tag-leetcode","tag-33","tag-87"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/939","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=939"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/939\/revisions"}],"predecessor-version":[{"id":940,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/939\/revisions\/940"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}