{"id":49272,"date":"2024-12-02T13:46:20","date_gmt":"2024-12-02T05:46:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49272\/"},"modified":"2024-12-02T13:46:20","modified_gmt":"2024-12-02T05:46:20","slug":"n-%e5%8f%89%e6%a0%91%e9%82%ae%e8%b4%ad%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49272\/","title":{"rendered":"N \u53c9\u6811\u90ae\u8d2d\u904d\u5386"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>N \u53c9\u6811\u90ae\u8d2d\u904d\u5386<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aN \u53c9\u6811\u90ae\u8d2d\u904d\u5386\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>590\u3002 n \u53c9\u6811\u540e\u5e8f\u904d\u5386<\/p>\n<p><strong>\u96be\u5ea6\uff1a<\/strong>\u7b80\u5355<\/p>\n<p><strong>\u4e3b\u9898\uff1a<\/strong> \u5806\u6808\u3001\u6811\u3001\u6df1\u5ea6\u4f18\u5148\u641c\u7d22<\/p>\n<p>\u7ed9\u5b9an\u53c9\u6811\u7684\u6839\uff0c\u8fd4\u56de<em>\u5176\u8282\u70b9\u503c\u7684\u540e\u5e8f\u904d\u5386<\/em>.<\/p>\n<p>nary-tree \u8f93\u5165\u5e8f\u5217\u5316\u4ee5\u5176\u7ea7\u522b\u987a\u5e8f\u904d\u5386\u6765\u8868\u793a\u3002\u6bcf\u7ec4\u5b69\u5b50\u90fd\u7531\u7a7a\u503c\u5206\u9694\uff08\u53c2\u89c1\u793a\u4f8b\uff09<\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/173235301867419bfaa396c.jpg\" class=\"aligncenter\" title=\"N \u53c9\u6811\u90ae\u8d2d\u904d\u5386\u63d2\u56fe\" alt=\"N \u53c9\u6811\u90ae\u8d2d\u904d\u5386\u63d2\u56fe\" \/><\/p>\n<ul>\n<li> <strong>\u8f93\u5165\uff1a<\/strong> root = [1,null,3,2,4,null,5,6]<\/li>\n<li> <strong>\u8f93\u51fa\uff1a<\/strong> [5,6,3,2,4,1]<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b2\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/173235301967419bfbbcee6.jpg\" class=\"aligncenter\" title=\"N \u53c9\u6811\u90ae\u8d2d\u904d\u5386\u63d2\u56fe1\" alt=\"N \u53c9\u6811\u90ae\u8d2d\u904d\u5386\u63d2\u56fe1\" \/><\/p>\n<ul>\n<li> <strong>\u8f93\u5165\uff1a<\/strong> root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13 ,\u7a7a,\u7a7a,14]<\/li>\n<li> <strong>\u8f93\u51fa\uff1a<\/strong> [2,6,14,11,7,3,12,8,4,13,9,10,5,1]<\/li>\n<\/ul>\n<p><strong>\u9650\u5236\uff1a<\/strong><\/p>\n<ul>\n<li>\u6811\u4e2d\u8282\u70b9\u7684\u6570\u91cf\u5728 [0, 100<sup>4<\/sup>] \u8303\u56f4\u5185\u3002<\/li>\n<li>-100 &lt;= node.val &lt;= 100<sup>4<\/sup><\/li>\n<li>n\u53c9\u6811\u7684\u9ad8\u5ea6\u5c0f\u4e8e\u7b49\u4e8e1000\u3002<\/li>\n<\/ul>\n<p><strong>\u540e\u7eed\uff1a<\/strong> \u9012\u5f52\u89e3\u51b3\u65b9\u6848\u5f88\u7b80\u5355\uff0c\u4f60\u80fd\u8fed\u4ee3\u5730\u5b8c\u6210\u5417\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u9012\u5f52\u548c\u8fed\u4ee3\u5730\u5904\u7406\u5b83\u3002\u7531\u4e8e\u540e\u7eed\u8981\u6c42\u8fed\u4ee3\u89e3\u51b3\u65b9\u6848\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u5173\u6ce8\u8fd9\u4e00\u70b9\u3002\u540e\u5e8f\u904d\u5386\u662f\u6307\u5148\u8bbf\u95ee\u5b50\u8282\u70b9\uff0c\u518d\u8bbf\u95ee\u7236\u8282\u70b9\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u7528 php \u5b9e\u73b0\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a<strong>590\u3002 n \u53c9\u6811\u540e\u5e8f\u904d\u5386<\/strong><\/p>\n<pre>&lt;?php\n\/\/Definition for a Node.\nclass Node {\n    public $val = null;\n    public $children = [];\n\n    public function __construct($val) {\n        $this-&gt;val = $val;\n    }\n}\n\n\/**\n * @param Node $root\n * @return integer[]\n *\/\nfunction postorder($root) {\n    ...\n    ...\n    ...\n    \/**\n     * go to .\/solution.php\n     *\/\n}\n\n\/\/ Example 1:\n$root1 = new Node(1);\n$root1-&gt;children = [\n    $node3 = new Node(3),\n    new Node(2),\n    new Node(4)\n];\n$node3-&gt;children = [\n    new Node(5),\n    new Node(6)\n];\n\nprint_r(postorder($root1)); \/\/ Output: [5, 6, 3, 2, 4, 1]\n\n\/\/ Example 2:\n$root2 = new Node(1);\n$root2-&gt;children = [\n    new Node(2),\n    $node3 = new Node(3),\n    $node4 = new Node(4),\n    $node5 = new Node(5)\n];\n$node3-&gt;children = [\n    $node6 = new Node(6),\n    $node7 = new Node(7)\n];\n$node4-&gt;children = [\n    $node8 = new Node(8)\n];\n$node5-&gt;children = [\n    $node9 = new Node(9),\n    $node10 = new Node(10)\n];\n$node7-&gt;children = [\n    new Node(11)\n];\n$node8-&gt;children = [\n    new Node(12)\n];\n$node9-&gt;children = [\n    new Node(13)\n];\n$node11 = $node7-&gt;children[0];\n$node11-&gt;children = [\n    new Node(14)\n];\n\nprint_r(postorder($root2)); \/\/ Output: [2, 6, 14, 11, 7, 3, 12, 8, 4, 13, 9, 10, 5, 1]\n?&gt;\n<\/pre>\n<h3> \u89e3\u91ca\uff1a <\/h3>\n<ol>\n<li>\n<p><strong>\u521d\u59cb\u5316<\/strong>:<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u5806\u6808\u5e76\u5c06\u6839\u8282\u70b9\u538b\u5165\u5176\u4e2d\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\u7ed3\u679c\u6765\u5b58\u50a8\u6700\u7ec8\u7684\u540e\u5e8f\u904d\u5386\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7a7f\u8d8a<\/strong>:<\/p>\n<ul>\n<li>\u4ece\u5806\u6808\u4e2d\u5f39\u51fa\u8282\u70b9\uff0c\u5c06\u5176\u503c\u63d2\u5165\u5230\u7ed3\u679c\u6570\u7ec4\u7684\u5f00\u5934\u3002<\/li>\n<li>\u5c06\u5176\u6240\u6709\u5b50\u9879\u63a8\u5165\u5806\u6808\u3002<\/li>\n<li>\u7ee7\u7eed\u76f4\u5230\u5806\u6808\u4e3a\u7a7a\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5faa\u73af\u7ed3\u675f\u540e\uff0c\u7ed3\u679c\u6570\u7ec4\u5c06\u5305\u542b\u540e\u5e8f\u7684\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u8fd9\u79cd\u8fed\u4ee3\u65b9\u6cd5\u901a\u8fc7\u4f7f\u7528\u5806\u6808\u5e76\u53cd\u8f6c\u901a\u5e38\u901a\u8fc7\u9012\u5f52\u5b8c\u6210\u7684\u8fc7\u7a0b\u6765\u6709\u6548\u5730\u6a21\u62df\u540e\u5e8f\u904d\u5386\u3002<\/p>\n<p><strong>\u8054\u7cfb\u94fe\u63a5<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u53d1\u73b0\u672c\u7cfb\u5217\u6709\u5e2e\u52a9\uff0c\u8bf7\u8003\u8651\u5728 github \u4e0a\u7ed9<strong>\u5b58\u50a8\u5e93<\/strong> \u4e00\u9897\u661f\uff0c\u6216\u5728\u60a8\u6700\u559c\u6b22\u7684\u793e\u4ea4\u7f51\u7edc\u4e0a\u5206\u4eab\u8be5\u5e16\u5b50?\u3002\u60a8\u7684\u652f\u6301\u5bf9\u6211\u6765\u8bf4\u610f\u4e49\u91cd\u5927\uff01<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u8981\u66f4\u591a\u7c7b\u4f3c\u7684\u6709\u7528\u5185\u5bb9\uff0c\u8bf7\u968f\u65f6\u5173\u6ce8\u6211\uff1a<\/p>\n<ul>\n<li><strong>\u9886\u82f1<\/strong><\/li>\n<li><strong>github<\/strong><\/li>\n<\/ul>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aN \u53c9\u6811\u90ae\u8d2d\u904d\u5386\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>N \u53c9\u6811\u90ae\u8d2d\u904d\u5386 \u6536\u85cf \u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aN \u53c9\u6811\u90ae\u8d2d\u904d\u5386\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002 590\u3002 n \u53c9\u6811\u540e\u5e8f\u904d\u5386 \u96be\u5ea6\uff1a\u7b80\u5355 \u4e3b\u9898\uff1a \u5806\u6808\u3001\u6811\u3001\u6df1\u5ea6\u4f18\u5148\u641c\u7d22 \u7ed9\u5b9an\u53c9\u6811\u7684\u6839\uff0c\u8fd4\u56de\u5176\u8282\u70b9\u503c\u7684\u540e\u5e8f\u904d\u5386. nary-tree \u8f93\u5165\u5e8f\u5217\u5316\u4ee5\u5176\u7ea7\u522b\u987a\u5e8f\u904d\u5386\u6765\u8868\u793a\u3002\u6bcf\u7ec4\u5b69\u5b50\u90fd\u7531\u7a7a\u503c\u5206\u9694\uff08\u53c2\u89c1\u793a\u4f8b\uff09 \u793a\u4f8b1\uff1a \u8f93\u5165\uff1a root = [1,null,3,2,4,null,5,6] \u8f93\u51fa\uff1a [5,6,3,2,4,1] \u793a\u4f8b2\uff1a \u8f93\u5165\uff1a root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13 ,\u7a7a,\u7a7a,14] \u8f93\u51fa\uff1a [2,6,14,11,7,3,12,8,4,13,9,10,5,1] \u9650\u5236\uff1a \u6811\u4e2d\u8282\u70b9\u7684\u6570\u91cf\u5728 [0, 1004] \u8303\u56f4\u5185\u3002 -100 &lt;= node.val &lt;= 1004 n\u53c9\u6811\u7684\u9ad8\u5ea6\u5c0f\u4e8e\u7b49\u4e8e1000\u3002 \u540e\u7eed\uff1a \u9012\u5f52\u89e3\u51b3\u65b9\u6848\u5f88\u7b80\u5355\uff0c\u4f60\u80fd\u8fed\u4ee3\u5730\u5b8c\u6210\u5417\uff1f \u89e3\u51b3\u65b9\u6848\uff1a \u6211\u4eec\u53ef\u4ee5\u9012\u5f52\u548c\u8fed\u4ee3\u5730\u5904\u7406\u5b83\u3002\u7531\u4e8e\u540e\u7eed\u8981\u6c42\u8fed\u4ee3\u89e3\u51b3\u65b9\u6848\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u5173\u6ce8\u8fd9\u4e00\u70b9\u3002\u540e\u5e8f\u904d\u5386\u662f\u6307\u5148\u8bbf\u95ee\u5b50\u8282\u70b9\uff0c\u518d\u8bbf\u95ee\u7236\u8282\u70b9\u3002 \u8ba9\u6211\u4eec\u7528 php \u5b9e\u73b0\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a590\u3002 n \u53c9\u6811\u540e\u5e8f\u904d\u5386 &lt;?php \/\/Definition for a Node. class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49272","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49272"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49272\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}