{"id":56,"date":"2025-06-15T12:35:37","date_gmt":"2025-06-15T04:35:37","guid":{"rendered":"http:\/\/116.62.30.57\/?p=56"},"modified":"2025-06-16T13:39:15","modified_gmt":"2025-06-16T05:39:15","slug":"%e6%af%8f%e6%97%a5%e4%b8%80%e9%a2%98-%e6%a0%91%e7%9a%84%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/qiuqiqaq.fun\/index.php\/2025\/06\/15\/%e6%af%8f%e6%97%a5%e4%b8%80%e9%a2%98-%e6%a0%91%e7%9a%84%e9%81%8d%e5%8e%86\/","title":{"rendered":"\u6bcf\u65e5\u4e00\u9898\u2014\u2014\u6811\u7684\u904d\u5386"},"content":{"rendered":"\n<p>\u9996\u5148\u4ec0\u4e48\u662f\u6811\uff1f\u6811\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u6700\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u5b83\u6a21\u62df\u4e86\u73b0\u5b9e\u4e16\u754c\u4e2d\u6811\u72b6\u7684\u5c42\u6b21\u7ed3\u6784\u5173\u7cfb\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6811\u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<p><strong>\u6811<\/strong>\u662f\u7531\u8282\u70b9\u548c\u8fb9\u7ec4\u6210\u7684\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u6839\u8282\u70b9\uff08root\uff09<\/li>\n\n\n\n<li>\u9664\u6839\u8282\u70b9\u5916\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u7236\u8282\u70b9<\/li>\n\n\n\n<li>\u8282\u70b9\u95f4\u4e0d\u80fd\u5f62\u6210\u73af\u8def<\/li>\n\n\n\n<li>\u4ece\u6839\u8282\u70b9\u5230\u4efb\u610f\u8282\u70b9\u90fd\u6709\u552f\u4e00\u8def\u5f84<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6811\u7684\u5b58\u50a8\u65b9\u5f0f<\/h2>\n\n\n\n<p><strong>\u94fe\u5f0f\u5b58\u50a8<\/strong>\uff1a\u4f7f\u7528\u6307\u9488\u8fde\u63a5\u8282\u70b9<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">c<code>struct TreeNode {\n    int data;\n    struct TreeNode* left;\n    struct TreeNode* right;\n};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u57fa\u672c\u64cd\u4f5c<\/h2>\n\n\n\n<p><strong>\u904d\u5386\u65b9\u5f0f<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u524d\u5e8f\u904d\u5386\uff1a\u6839 \u2192 \u5de6 \u2192 \u53f3<\/li>\n\n\n\n<li>\u4e2d\u5e8f\u904d\u5386\uff1a\u5de6 \u2192 \u6839 \u2192 \u53f3<\/li>\n\n\n\n<li>\u540e\u5e8f\u904d\u5386\uff1a\u5de6 \u2192 \u53f3 \u2192 \u6839<\/li>\n\n\n\n<li>\u5c42\u5e8f\u904d\u5386\uff1a\u6309\u5c42\u6b21\u4ece\u4e0a\u5230\u4e0b\uff0c\u4ece\u5de6\u5230\u53f3<\/li>\n<\/ul>\n\n\n\n<p><strong>\u57fa\u672c\u64cd\u4f5c<\/strong>\uff1a\u63d2\u5165\u3001\u5220\u9664\u3001\u67e5\u627e\u3001\u8ba1\u7b97\u9ad8\u5ea6\u3001\u5224\u65ad\u5e73\u8861\u7b49<\/p>\n\n\n\n<p>\u6709\u4e86\u4e0a\u9762\u7684\u57fa\u7840\u540e\u4fbf\u53ef\u4ee5\u5c1d\u8bd5\u505a\u9898\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386<\/h2>\n\n\n\n<p>\u9898\u76ee\u63cf\u8ff0\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u00a0<code>root<\/code>\u00a0\uff0c\u8fd4\u56de\u00a0<em>\u5b83\u7684\u00a0<strong>\u4e2d\u5e8f<\/strong>\u00a0\u904d\u5386<\/em>\u00a0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u793a\u4f8b 1\uff1a<br>\u8f93\u5165\uff1aroot = [1,null,2,3]<br>\u8f93\u51fa\uff1a[1,3,2]<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u793a\u4f8b 2\uff1a<br><br>\u8f93\u5165\uff1aroot = []<br>\u8f93\u51fa\uff1a[]<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u793a\u4f8b 3\uff1a<br><br>\u8f93\u5165\uff1aroot = [1]<br>\u8f93\u51fa\uff1a[1]<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u63d0\u793a\uff1a<br><br>\u6811\u4e2d\u8282\u70b9\u6570\u76ee\u5728\u8303\u56f4 [0, 100] \u5185<br>-100 &lt;= Node.val &lt;= 100<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u89e3\u9898\u601d\u8def\">\u89e3\u9898\u601d\u8def<\/h3>\n\n\n\n<p>\u9012\u5f52\u6cd5<br>1,\u7ed3\u675f\u6761\u4ef6 root==NULL \u5219\u8fd4\u56de<br>2,\u9012\u5f52\u5de6\u652f<br>3,\u5904\u7406\u5f53\u524d\u8282\u70b9<br>4,\u9012\u5f52\u53f3\u652f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u5b9e\u73b0<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n *\/\n\/**\n * Note: The returned array must be malloced, assume caller calls free().\n *\/\nvoid visit(struct TreeNode* root, int* returnSize, int* ret){\n    \/\/\u8282\u70b9\u4e3a\u7a7a\u65f6\u8fd4\u56de\n    if(root == NULL){\n        return;\n    }\n    \/\/\u9012\u5f52\u5904\u7406\u5de6\u5b50\u6811\n    if(root->left != NULL){\n        visit(root->left, returnSize, ret);\n    }\n    \/\/\u5904\u7406\u5f53\u524d\u8282\u70b9\n    ret&#91;*returnSize] = root->val;\n    (*returnSize)++;\n    \/\/\u9012\u5f52\u5904\u7406\u53f3\u5b50\u6811\n    if(root->right != NULL){\n        visit(root->right, returnSize, ret);\n    }\n}\n\n\nint* inorderTraversal(struct TreeNode* root, int* returnSize) {\n    \/\/ \u521d\u59cb\u6570\u7ec4\n    int* ret = (int*)malloc(100*sizeof(int));\n    *returnSize = 0;\n    \/\/ \u6839\u8282\u70b9\u4e3a\u7a7a\u65f6\u76f4\u63a5\u8fd4\u56de\n    if(root == NULL)\n        return ret;\n    visit(root, returnSize, ret);\n    return ret;\n}<\/code><\/pre>\n\n\n\n<p>\u5148\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u7684\u65b9\u6cd5\u4e5f\u4e0e\u6b64\u7c7b\u4f3c\uff0c\u9664\u4e86\u9012\u5f52\uff0c\u7528\u8fed\u4ee3\u6765\u89e3\u51b3\u8be5\u95ee\u9898\u4e5f\u662f\u4e0d\u9519\u7684\u9009\u62e9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n *\/\n\n\n\/**\n * Note: The returned array must be malloced, assume caller calls free().\n *\/\n\n\/\/\u65b9\u6cd5\u4e8c\uff1a\u8fed\u4ee3\u6cd5\n\/\/1,\u904d\u5386\u8282\u70b9A\uff0c\u5c06\u8282\u70b9\u538b\u5165\u6808\u4e2d\uff0c\n\/\/2,\u904d\u5386A\u7684\u5de6\u652f\uff0c\n\/\/3,A\u51fa\u6808\uff0c\u8bbf\u95eeA\n\/\/4,\u904d\u5386A\u7684\u53f3\u652f\nint* inorderTraversal(struct TreeNode* root, int* returnSize){\n    int     iMax        = 100;\n    int     iTop        = 0;\n    int*    pRet        = NULL;\n    int     iRetSize    = 0;\n\n    struct TreeNode*    pTmp    = root;\n    struct TreeNode*    pStrTreeBuf&#91;iMax];       \/\/\u5efa\u7acb\u8282\u70b9\u6307\u9488\u6570\u7ec4\uff0c\u6a21\u62df\u6808\u4fdd\u5b58\u8282\u70b9\n\n    pRet = (int*)malloc(sizeof(int) * iMax);\n    memset(pRet, 0x00, sizeof(int) * iMax);\n\n    while((pTmp != NULL) || (iTop != 0))\n    {\n        while(pTmp != NULL)\n        {\n            \/\/1,\u904d\u5386\u8282\u70b9\uff0c\u5c06\u68c0\u70b9\u538b\u5165\u6808\u4e2d\n            pStrTreeBuf&#91;iTop] = pTmp;\n            iTop += 1;\n\n            \/\/2,\u904d\u5386\u5de6\u652f\n            pTmp = pTmp->left;\n        }\n\n        \/\/3,\u51fa\u6808\uff0c\u8bbf\u95ee\u8282\u70b9\n        iTop -= 1;\n        pTmp = pStrTreeBuf&#91;iTop];\n        pRet&#91;iRetSize] = pTmp->val;\n        iRetSize += 1;\n\n        \/\/4,\u904d\u5386\u53f3\u652f\n        pTmp = pTmp->right;\n    }\n\n    \/\/5,\u8fd4\u56de\n    *returnSize = iRetSize;\n    return pRet;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ca1\u60f3\u5230\u4f60\u5c45\u7136&#8230;&#8230;&#8230;..<\/p>\n","protected":false},"author":1,"featured_media":30,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,11],"tags":[10],"class_list":["post-56","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-leetcode","category-datastructure","tag-10"],"_links":{"self":[{"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":7,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":63,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions\/63"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/media\/30"}],"wp:attachment":[{"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qiuqiqaq.fun\/index.php\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}