{"id":47086,"date":"2024-12-02T15:14:22","date_gmt":"2024-12-02T07:14:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47086\/"},"modified":"2024-12-02T15:14:22","modified_gmt":"2024-12-02T07:14:22","slug":"php-%e9%80%92%e5%bd%92%e5%87%bd%e6%95%b0%e5%9c%a8%e6%a0%91%e5%bd%a2%e7%bb%93%e6%9e%84%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47086\/","title":{"rendered":"PHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p>\u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u63d0\u4f9b\u4e86\u4e00\u79cd\u901a\u7528\u7684\u904d\u5386\u65b9\u6cd5\uff1a\u5b9a\u4e49\u9012\u5f52\u51fd\u6570\uff0c\u4ee5\u8282\u70b9\u4e3a\u53c2\u6570\uff0c\u5305\u542b\u57fa\u7ebf\u6761\u4ef6\u548c\u9012\u5f52\u8c03\u7528\u3002\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386\u6811\uff0c\u5e76\u5728\u6bcf\u4e2a\u8282\u70b9\u6267\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u6253\u5370\u6570\u636e\u6216\u8ba1\u7b97\u5b50\u6811\u548c\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u8ba1\u7b97\u4e8c\u53c9\u6811\u6df1\u5ea6\u7684\u9012\u5f52\u51fd\u6570\u9012\u5f52\u8ba1\u7b97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u6df1\u5ea6\uff0c\u5e76\u8fd4\u56de\u6700\u5927\u6df1\u5ea6\u52a0\u4e0a 1\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729865295671ba64f1d7ab.jpg\" class=\"aligncenter\" title=\"PHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528\u63d2\u56fe\" alt=\"PHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528<\/h2>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd puissante \u7684\u7f16\u7a0b\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u3002\u5728\u5904\u7406\u6811\u5f62\u7ed3\u6784\u65f6\uff0c\u9012\u5f52\u51fd\u6570\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u6811\u5f62\u7ed3\u6784\u672c\u8d28\u4e0a\u662f\u9012\u5f52\u7684\u3002<\/p>\n<p><strong>\u6811\u5f62\u7ed3\u6784<\/strong><\/p>\n<p>\u6811\u5f62\u7ed3\u6784\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u53ef\u4ee5\u6709\u591a\u4e2a\u5b50\u8282\u70b9\u3002\u6839\u8282\u70b9\u662f\u6ca1\u6709\u7236\u8282\u70b9\u7684\u9876\u5c42\u8282\u70b9\u3002\u5b50\u8282\u70b9\u53ef\u4ee5\u6709\u591a\u4e2a\u7236\u8282\u70b9\uff0c\u5f62\u6210\u6811\u72b6\u7ed3\u6784\u3002<\/p>\n<p><strong>\u9012\u5f52\u51fd\u6570<\/strong><\/p>\n<p>\u9012\u5f52\u51fd\u6570\u662f\u4e00\u4e2a\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u7684\u51fd\u6570\u3002\u9012\u5f52\u51fd\u6570\u901a\u5e38\u4ee5\u4ee5\u4e0b\u5f62\u5f0f\u5b9a\u4e49\uff1a<\/p>\n<pre>function recurse($parameter) {\n  \/\/ \u57fa\u7ebf\u6761\u4ef6\n  if ($parameter satisfies the base condition) {\n    return the result;\n  } else {\n    \/\/ \u9012\u5f52\u8c03\u7528\n    $result = recurse($new_parameter);\n    \/\/ \u5904\u7406\u7ed3\u679c\n    return $processed_result;\n  }\n}<\/pre>\n<p><strong>\u6811\u5f62\u7ed3\u6784\u7684\u904d\u5386<\/strong><\/p>\n<p>\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386\u6811\u5f62\u7ed3\u6784\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6280\u672f\u3002\u4ee5\u4e0b\u662f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u9012\u5f52\u51fd\u6570\uff1a<\/p>\n<pre>function traverseTree($node) {\n  \/\/ \u57fa\u7ebf\u6761\u4ef6\uff1a\u5982\u679c\u8282\u70b9\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de\n  if ($node === null) {\n    return;\n  }\n\n  \/\/ \u5728\u904d\u5386\u8282\u70b9\u4e4b\u524d\u6267\u884c\u64cd\u4f5c\n  echo $node-&gt;data . PHP_EOL;\n\n  \/\/ \u9012\u5f52\u8c03\u7528\u5de6\u5b50\u6811\n  traverseTree($node-&gt;left);\n\n  \/\/ \u5728\u904d\u5386\u8282\u70b9\u4e4b\u540e\u6267\u884c\u64cd\u4f5c\n  \/\/ \u4f8b\u5982\uff0c\u53ef\u4ee5\u8ba1\u7b97\u8282\u70b9\u7684\u5b50\u6811\u548c\n\n  \/\/ \u9012\u5f52\u8c03\u7528\u53f3\u5b50\u6811\n  traverseTree($node-&gt;right);\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u8ba1\u7b97\u4e8c\u53c9\u6811\u7684\u6df1\u5ea6<\/strong><\/p>\n<p>\u4e3a\u4e86\u5c55\u793a\u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5b9e\u9645\u5e94\u7528\uff0c\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u8ba1\u7b97\u4e8c\u53c9\u6811\u6df1\u5ea6\u7684\u793a\u4f8b\u3002<\/p>\n<pre>function treeDepth($node) {\n  \/\/ \u57fa\u7ebf\u6761\u4ef6\uff1a\u5982\u679c\u8282\u70b9\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de 0\n  if ($node === null) {\n    return 0;\n  }\n\n  \/\/ \u8ba1\u7b97\u5de6\u5b50\u6811\u7684\u6df1\u5ea6\n  $leftDepth = treeDepth($node-&gt;left);\n\n  \/\/ \u8ba1\u7b97\u53f3\u5b50\u6811\u7684\u6df1\u5ea6\n  $rightDepth = treeDepth($node-&gt;right);\n\n  \/\/ \u8fd4\u56de\u8f83\u5927\u6df1\u5ea6 + 1\n  return max($leftDepth, $rightDepth) + 1;\n}<\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8c03\u7528\u8be5\u51fd\u6570\uff1a<\/p>\n<pre>$root = createBinaryTree();\necho \"Tree depth: \" . treeDepth($root);<\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5c06\u9012\u5f52\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u8fd4\u56de\u6811\u7684\u6df1\u5ea6\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528 \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u63d0\u4f9b\u4e86\u4e00\u79cd\u901a\u7528\u7684\u904d\u5386\u65b9\u6cd5\uff1a\u5b9a\u4e49\u9012\u5f52\u51fd\u6570\uff0c\u4ee5\u8282\u70b9\u4e3a\u53c2\u6570\uff0c\u5305\u542b\u57fa\u7ebf\u6761\u4ef6\u548c\u9012\u5f52\u8c03\u7528\u3002\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386\u6811\uff0c\u5e76\u5728\u6bcf\u4e2a\u8282\u70b9\u6267\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u6253\u5370\u6570\u636e\u6216\u8ba1\u7b97\u5b50\u6811\u548c\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u8ba1\u7b97\u4e8c\u53c9\u6811\u6df1\u5ea6\u7684\u9012\u5f52\u51fd\u6570\u9012\u5f52\u8ba1\u7b97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u6df1\u5ea6\uff0c\u5e76\u8fd4\u56de\u6700\u5927\u6df1\u5ea6\u52a0\u4e0a 1\u3002 PHP \u9012\u5f52\u51fd\u6570\u5728\u6811\u5f62\u7ed3\u6784\u4e2d\u7684\u5e94\u7528 \u5f15\u8a00 \u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd puissante \u7684\u7f16\u7a0b\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u3002\u5728\u5904\u7406\u6811\u5f62\u7ed3\u6784\u65f6\uff0c\u9012\u5f52\u51fd\u6570\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u6811\u5f62\u7ed3\u6784\u672c\u8d28\u4e0a\u662f\u9012\u5f52\u7684\u3002 \u6811\u5f62\u7ed3\u6784 \u6811\u5f62\u7ed3\u6784\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u53ef\u4ee5\u6709\u591a\u4e2a\u5b50\u8282\u70b9\u3002\u6839\u8282\u70b9\u662f\u6ca1\u6709\u7236\u8282\u70b9\u7684\u9876\u5c42\u8282\u70b9\u3002\u5b50\u8282\u70b9\u53ef\u4ee5\u6709\u591a\u4e2a\u7236\u8282\u70b9\uff0c\u5f62\u6210\u6811\u72b6\u7ed3\u6784\u3002 \u9012\u5f52\u51fd\u6570 \u9012\u5f52\u51fd\u6570\u662f\u4e00\u4e2a\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u7684\u51fd\u6570\u3002\u9012\u5f52\u51fd\u6570\u901a\u5e38\u4ee5\u4ee5\u4e0b\u5f62\u5f0f\u5b9a\u4e49\uff1a function recurse($parameter) { \/\/ \u57fa\u7ebf\u6761\u4ef6 if ($parameter satisfies the base condition) { return the result; } else { \/\/ \u9012\u5f52\u8c03\u7528 $result = recurse($new_parameter); \/\/ \u5904\u7406\u7ed3\u679c return $processed_result; } } \u6811\u5f62\u7ed3\u6784\u7684\u904d\u5386 \u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386\u6811\u5f62\u7ed3\u6784\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6280\u672f\u3002\u4ee5\u4e0b\u662f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u9012\u5f52\u51fd\u6570\uff1a function traverseTree($node) { \/\/ \u57fa\u7ebf\u6761\u4ef6\uff1a\u5982\u679c\u8282\u70b9\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de if ($node === [&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-47086","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47086","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=47086"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47086\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}