{"id":1083084,"date":"2025-01-08T12:54:42","date_gmt":"2025-01-08T04:54:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1083084.html"},"modified":"2025-01-08T12:54:44","modified_gmt":"2025-01-08T04:54:44","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e4%ba%8c%e5%8f%89%e6%a0%91-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1083084.html","title":{"rendered":"python\u5982\u4f55\u8868\u793a\u4e8c\u53c9\u6811"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193825\/7d819bb7-4896-4a68-9b3d-88955f58cfca.webp\" alt=\"python\u5982\u4f55\u8868\u793a\u4e8c\u53c9\u6811\" \/><\/p>\n<p><p> <strong>\u4e00\u3001Python\u5982\u4f55\u8868\u793a\u4e8c\u53c9\u6811<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8868\u793a\u4e8c\u53c9\u6811\u7684\u5e38\u7528\u65b9\u6cd5\u6709<strong>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528\u5b57\u5178<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u66f4\u597d\u5730\u8868\u793a\u6811\u7684\u7ed3\u6784\u548c\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u6765\u5b9a\u4e49\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u3002\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e09\u4e2a\u5c5e\u6027\uff1a<strong>\u503c\u3001\u5de6\u5b50\u8282\u70b9\u3001\u53f3\u5b50\u8282\u70b9<\/strong>\u3002\u8fd9\u6837\u53ef\u4ee5\u6e05\u6670\u5730\u8868\u793a\u51fa\u6bcf\u4e2a\u8282\u70b9\u7684\u503c\u53ca\u5176\u5b50\u8282\u70b9\u7684\u5173\u7cfb\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TreeNode:<\/p>\n<p>    def __init__(self, val=0, left=None, right=None):<\/p>\n<p>        self.val = val<\/p>\n<p>        self.left = left<\/p>\n<p>        self.right = right<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u89e3\u6790<\/strong>\uff1a\u5b9a\u4e49\u4e00\u4e2a\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b<code>TreeNode<\/code>\uff0c\u5176\u4e2d<code>__init__<\/code>\u65b9\u6cd5\u521d\u59cb\u5316\u8282\u70b9\u7684\u503c<code>val<\/code>\uff0c\u5de6\u5b50\u8282\u70b9<code>left<\/code>\u548c\u53f3\u5b50\u8282\u70b9<code>right<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u8868\u793a\u4e8c\u53c9\u6811\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6e05\u6670\u7684\u7ed3\u6784<\/strong>\uff1a\u901a\u8fc7\u7c7b\u7684\u5b9a\u4e49\uff0c\u53ef\u4ee5\u6e05\u6670\u5730\u8868\u8fbe\u6811\u7684\u5c42\u6b21\u5173\u7cfb\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u5de6\u3001\u53f3\u5b50\u8282\u70b9\u90fd\u660e\u786e\u8868\u793a\u3002<\/li>\n<li><strong>\u6613\u4e8e\u6269\u5c55<\/strong>\uff1a\u53ef\u4ee5\u5728\u7c7b\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6811\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u5220\u9664\u3001\u67e5\u627e\u7b49\u3002<\/li>\n<li><strong>\u9762\u5411\u5bf9\u8c61\u7684\u601d\u60f3<\/strong>\uff1a\u7b26\u5408\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u601d\u60f3\uff0c\u4fbf\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u7684\u65b9\u5f0f\u6765\u8868\u793a\u4e8c\u53c9\u6811\uff0c\u6bcf\u4e2a\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u8282\u70b9\u7684\u503c\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u5de6\u5b50\u8282\u70b9\uff0c\u7b2c\u4e09\u4e2a\u5143\u7d20\u662f\u53f3\u5b50\u8282\u70b9\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u8868\u793a\u5c0f\u89c4\u6a21\u7684\u4e8c\u53c9\u6811\u3002\u5177\u4f53\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">binary_tree = [1, [2, [4, None, None], [5, None, None]], [3, [6, None, None], [7, None, None]]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>\uff1a\u5728\u8fd9\u4e2a\u5d4c\u5957\u5217\u8868\u4e2d\uff0c\u6839\u8282\u70b9\u7684\u503c\u662f<code>1<\/code>\uff0c\u5176\u5de6\u5b50\u8282\u70b9\u662f\u4e00\u4e2a\u503c\u4e3a<code>2<\/code>\u7684\u8282\u70b9\uff0c\u53f3\u5b50\u8282\u70b9\u662f\u4e00\u4e2a\u503c\u4e3a<code>3<\/code>\u7684\u8282\u70b9\u3002\u4ee5\u6b64\u7c7b\u63a8\uff0c\u53ef\u4ee5\u8868\u793a\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u76f4\u89c2<\/strong>\uff1a\u9002\u5408\u8868\u793a\u5c0f\u89c4\u6a21\u7684\u4e8c\u53c9\u6811\uff0c\u7ed3\u6784\u6e05\u6670\u3002<\/li>\n<li><strong>\u65e0\u9700\u5b9a\u4e49\u7c7b<\/strong>\uff1a\u76f4\u63a5\u4f7f\u7528\u5217\u8868\u5373\u53ef\uff0c\u65e0\u9700\u989d\u5916\u5b9a\u4e49\u7c7b\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e0d\u9002\u5408\u5927\u89c4\u6a21\u4e8c\u53c9\u6811<\/strong>\uff1a\u5d4c\u5957\u5217\u8868\u8868\u793a\u5927\u89c4\u6a21\u4e8c\u53c9\u6811\u65f6\uff0c\u7ed3\u6784\u590d\u6742\uff0c\u96be\u4ee5\u7ef4\u62a4\u3002<\/li>\n<li><strong>\u64cd\u4f5c\u4e0d\u4fbf<\/strong>\uff1a\u8fdb\u884c\u63d2\u5165\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u65f6\uff0c\u4e0d\u5982\u7c7b\u548c\u5bf9\u8c61\u65b9\u5f0f\u65b9\u4fbf\u3002<\/li>\n<\/ol>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u7684\u65b9\u5f0f\u6765\u8868\u793a\u4e8c\u53c9\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u8282\u70b9\u7684\u503c\u3001\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u8868\u793a\u952e\u503c\u5bf9\u5173\u7cfb\u7684\u6811\u7ed3\u6784\u3002\u5177\u4f53\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">binary_tree = {<\/p>\n<p>    &#39;val&#39;: 1,<\/p>\n<p>    &#39;left&#39;: {<\/p>\n<p>        &#39;val&#39;: 2,<\/p>\n<p>        &#39;left&#39;: {&#39;val&#39;: 4, &#39;left&#39;: None, &#39;right&#39;: None},<\/p>\n<p>        &#39;right&#39;: {&#39;val&#39;: 5, &#39;left&#39;: None, &#39;right&#39;: None}<\/p>\n<p>    },<\/p>\n<p>    &#39;right&#39;: {<\/p>\n<p>        &#39;val&#39;: 3,<\/p>\n<p>        &#39;left&#39;: {&#39;val&#39;: 6, &#39;left&#39;: None, &#39;right&#39;: None},<\/p>\n<p>        &#39;right&#39;: {&#39;val&#39;: 7, &#39;left&#39;: None, &#39;right&#39;: None}<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>\uff1a\u5728\u8fd9\u4e2a\u5b57\u5178\u4e2d\uff0c\u6839\u8282\u70b9\u5305\u542b\u4e09\u4e2a\u952e\uff1a<code>val<\/code>\u8868\u793a\u8282\u70b9\u7684\u503c\uff0c<code>left<\/code>\u8868\u793a\u5de6\u5b50\u8282\u70b9\uff0c<code>right<\/code>\u8868\u793a\u53f3\u5b50\u8282\u70b9\u3002\u901a\u8fc7\u5d4c\u5957\u5b57\u5178\uff0c\u53ef\u4ee5\u8868\u793a\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7ed3\u6784\u6e05\u6670<\/strong>\uff1a\u9002\u5408\u8868\u793a\u952e\u503c\u5bf9\u5173\u7cfb\u7684\u6811\u7ed3\u6784\uff0c\u7ed3\u6784\u6e05\u6670\u3002<\/li>\n<li><strong>\u6613\u4e8e\u8bbf\u95ee<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u65b9\u4fbf\u5730\u8bbf\u95ee\u8282\u70b9\u7684\u503c\u3001\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e0d\u9002\u5408\u5927\u89c4\u6a21\u4e8c\u53c9\u6811<\/strong>\uff1a\u5d4c\u5957\u5b57\u5178\u8868\u793a\u5927\u89c4\u6a21\u4e8c\u53c9\u6811\u65f6\uff0c\u7ed3\u6784\u590d\u6742\uff0c\u96be\u4ee5\u7ef4\u62a4\u3002<\/li>\n<li><strong>\u64cd\u4f5c\u4e0d\u4fbf<\/strong>\uff1a\u8fdb\u884c\u63d2\u5165\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u65f6\uff0c\u4e0d\u5982\u7c7b\u548c\u5bf9\u8c61\u65b9\u5f0f\u65b9\u4fbf\u3002<\/li>\n<\/ol>\n<p><p><strong>\u56db\u3001\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4e86\u89e3\u4e86\u5982\u4f55\u8868\u793a\u4e8c\u53c9\u6811\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u4ecb\u7ecd\u4e00\u4e9b\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u5305\u62ec<strong>\u63d2\u5165\u8282\u70b9\u3001\u5220\u9664\u8282\u70b9\u3001\u67e5\u627e\u8282\u70b9\u3001\u904d\u5386\u4e8c\u53c9\u6811<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>\u63d2\u5165\u8282\u70b9<\/strong><\/p>\n<\/p>\n<p><p>\u63d2\u5165\u8282\u70b9\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9\u5230\u4e8c\u53c9\u6811\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def insert_node(root, val):<\/p>\n<p>    if root is None:<\/p>\n<p>        return TreeNode(val)<\/p>\n<p>    if val &lt; root.val:<\/p>\n<p>        root.left = insert_node(root.left, val)<\/p>\n<p>    else:<\/p>\n<p>        root.right = insert_node(root.right, val)<\/p>\n<p>    return root<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>root = TreeNode(10)<\/p>\n<p>insert_node(root, 5)<\/p>\n<p>insert_node(root, 15)<\/p>\n<p>insert_node(root, 3)<\/p>\n<p>insert_node(root, 7)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>\uff1a\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>insert_node<\/code>\u51fd\u6570\u9012\u5f52\u5730\u5c06\u65b0\u8282\u70b9\u63d2\u5165\u5230\u5408\u9002\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u6811\u4e3a\u7a7a\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u8282\u70b9\u4f5c\u4e3a\u6839\u8282\u70b9\uff1b\u5426\u5219\uff0c\u6839\u636e\u65b0\u8282\u70b9\u7684\u503c\u4e0e\u5f53\u524d\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u9012\u5f52\u5730\u63d2\u5165\u5230\u5de6\u5b50\u6811\u6216\u53f3\u5b50\u6811\u3002<\/p>\n<\/p>\n<p><p><strong>\u5220\u9664\u8282\u70b9<\/strong><\/p>\n<\/p>\n<p><p>\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5220\u9664\u4e00\u4e2a\u8282\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_node(root, val):<\/p>\n<p>    if root is None:<\/p>\n<p>        return root<\/p>\n<p>    if val &lt; root.val:<\/p>\n<p>        root.left = delete_node(root.left, val)<\/p>\n<p>    elif val &gt; root.val:<\/p>\n<p>        root.right = delete_node(root.right, val)<\/p>\n<p>    else:<\/p>\n<p>        if root.left is None:<\/p>\n<p>            return root.right<\/p>\n<p>        elif root.right is None:<\/p>\n<p>            return root.left<\/p>\n<p>        temp = min_value_node(root.right)<\/p>\n<p>        root.val = temp.val<\/p>\n<p>        root.right = delete_node(root.right, temp.val)<\/p>\n<p>    return root<\/p>\n<p>def min_value_node(node):<\/p>\n<p>    current = node<\/p>\n<p>    while current.left is not None:<\/p>\n<p>        current = current.left<\/p>\n<p>    return current<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>root = delete_node(root, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>\uff1a\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>delete_node<\/code>\u51fd\u6570\u9012\u5f52\u5730\u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\u3002\u5982\u679c\u8282\u70b9\u503c\u5c0f\u4e8e\u5f53\u524d\u8282\u70b9\u503c\uff0c\u5219\u9012\u5f52\u5730\u5728\u5de6\u5b50\u6811\u5220\u9664\u8282\u70b9\uff1b\u5982\u679c\u8282\u70b9\u503c\u5927\u4e8e\u5f53\u524d\u8282\u70b9\u503c\uff0c\u5219\u9012\u5f52\u5730\u5728\u53f3\u5b50\u6811\u5220\u9664\u8282\u70b9\uff1b\u5982\u679c\u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\uff0c\u6839\u636e\u5176\u5b50\u8282\u70b9\u60c5\u51b5\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u67e5\u627e\u8282\u70b9<\/strong><\/p>\n<\/p>\n<p><p>\u67e5\u627e\u8282\u70b9\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u67e5\u627e\u4e00\u4e2a\u8282\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def search_node(root, val):<\/p>\n<p>    if root is None or root.val == val:<\/p>\n<p>        return root<\/p>\n<p>    if val &lt; root.val:<\/p>\n<p>        return search_node(root.left, val)<\/p>\n<p>    return search_node(root.right, val)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>node = search_node(root, 7)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>\uff1a\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>search_node<\/code>\u51fd\u6570\u9012\u5f52\u5730\u67e5\u627e\u8282\u70b9\u3002\u5982\u679c\u627e\u5230\u8282\u70b9\u6216\u6811\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de\u8282\u70b9\uff1b\u5426\u5219\uff0c\u6839\u636e\u8282\u70b9\u503c\u4e0e\u5f53\u524d\u8282\u70b9\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u9012\u5f52\u5730\u5728\u5de6\u5b50\u6811\u6216\u53f3\u5b50\u6811\u67e5\u627e\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><p><strong>\u904d\u5386\u4e8c\u53c9\u6811<\/strong><\/p>\n<\/p>\n<p><p>\u904d\u5386\u4e8c\u53c9\u6811\u64cd\u4f5c\u5305\u62ec<strong>\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u3001\u5c42\u6b21\u904d\u5386<\/strong>\u3002\u4ee5\u4e0b\u662f\u5404\u4e2a\u904d\u5386\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u524d\u5e8f\u904d\u5386<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def preorder_traversal(root):<\/p>\n<p>    if root:<\/p>\n<p>        print(root.val, end=&#39; &#39;)<\/p>\n<p>        preorder_traversal(root.left)<\/p>\n<p>        preorder_traversal(root.right)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>preorder_traversal(root)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e2d\u5e8f\u904d\u5386<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def inorder_traversal(root):<\/p>\n<p>    if root:<\/p>\n<p>        inorder_traversal(root.left)<\/p>\n<p>        print(root.val, end=&#39; &#39;)<\/p>\n<p>        inorder_traversal(root.right)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>inorder_traversal(root)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u540e\u5e8f\u904d\u5386<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def postorder_traversal(root):<\/p>\n<p>    if root:<\/p>\n<p>        postorder_traversal(root.left)<\/p>\n<p>        postorder_traversal(root.right)<\/p>\n<p>        print(root.val, end=&#39; &#39;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>postorder_traversal(root)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c42\u6b21\u904d\u5386<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>def level_order_traversal(root):<\/p>\n<p>    if not root:<\/p>\n<p>        return<\/p>\n<p>    queue = deque([root])<\/p>\n<p>    while queue:<\/p>\n<p>        node = queue.popleft()<\/p>\n<p>        print(node.val, end=&#39; &#39;)<\/p>\n<p>        if node.left:<\/p>\n<p>            queue.append(node.left)<\/p>\n<p>        if node.right:<\/p>\n<p>            queue.append(node.right)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>level_order_traversal(root)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790<\/strong>\uff1a\u5728\u8fd9\u4e9b\u793a\u4f8b\u4e2d\uff0c\u5206\u522b\u5b9e\u73b0\u4e86\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u548c\u5c42\u6b21\u904d\u5386\u3002\u524d\u5e8f\u904d\u5386\u5148\u8bbf\u95ee\u6839\u8282\u70b9\uff0c\u518d\u8bbf\u95ee\u5de6\u5b50\u8282\u70b9\uff0c\u6700\u540e\u8bbf\u95ee\u53f3\u5b50\u8282\u70b9\uff1b\u4e2d\u5e8f\u904d\u5386\u5148\u8bbf\u95ee\u5de6\u5b50\u8282\u70b9\uff0c\u518d\u8bbf\u95ee\u6839\u8282\u70b9\uff0c\u6700\u540e\u8bbf\u95ee\u53f3\u5b50\u8282\u70b9\uff1b\u540e\u5e8f\u904d\u5386\u5148\u8bbf\u95ee\u5de6\u5b50\u8282\u70b9\uff0c\u518d\u8bbf\u95ee\u53f3\u5b50\u8282\u70b9\uff0c\u6700\u540e\u8bbf\u95ee\u6839\u8282\u70b9\uff1b\u5c42\u6b21\u904d\u5386\u4f7f\u7528\u961f\u5217\uff0c\u6309\u5c42\u6b21\u9010\u5c42\u8bbf\u95ee\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u7ed3\u8bba<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u5982\u4f55\u8868\u793a\u4e8c\u53c9\u6811\uff0c\u5305\u62ec\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528\u5b57\u5178\u4e09\u79cd\u5e38\u7528\u65b9\u6cd5\u3002\u5e76\u4e14\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u8282\u70b9\u3001\u5220\u9664\u8282\u70b9\u3001\u67e5\u627e\u8282\u70b9\u548c\u904d\u5386\u4e8c\u53c9\u6811\u3002\u901a\u8fc7\u8fd9\u4e9b\u5185\u5bb9\uff0c\u53ef\u4ee5\u638c\u63e1\u5728Python\u4e2d\u8868\u793a\u548c\u64cd\u4f5c\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u4e3a\u8fdb\u4e00\u6b65\u5b66\u4e60\u548c\u5e94\u7528\u6253\u4e0b\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e8c\u53c9\u6811\u8282\u70b9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6765\u8868\u793a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u3002\u901a\u5e38\u4f1a\u521b\u5efa\u4e00\u4e2a<code>TreeNode<\/code>\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u8282\u70b9\u7684\u503c\u4ee5\u53ca\u6307\u5411\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u7684\u5f15\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">class TreeNode:\n    def __init__(self, value):\n        self.value = value\n        self.left = None\n        self.right = None\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u57fa\u7840\u7ed3\u6784\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u904d\u5386\u4e8c\u53c9\u6811\uff1f<\/strong><br \/>\u904d\u5386\u4e8c\u53c9\u6811\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u5305\u62ec\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6216\u8fed\u4ee3\u7684\u65b9\u6cd5\u5b9e\u73b0\u8fd9\u4e9b\u904d\u5386\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e2d\u5e8f\u904d\u5386\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u9012\u5f52\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-python\">def inorder_traversal(node):\n    if node:\n        inorder_traversal(node.left)\n        print(node.value)\n        inorder_traversal(node.right)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u6309\u7167\u5de6\u5b50\u6811\u3001\u6839\u8282\u70b9\u3001\u53f3\u5b50\u6811\u7684\u987a\u5e8f\u8f93\u51fa\u8282\u70b9\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e8c\u53c9\u6811\u7684\u63d2\u5165\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u65b0\u8282\u70b9\u901a\u5e38\u9700\u8981\u9075\u5faa\u4e00\u5b9a\u7684\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u5de6\u5b50\u6811\u7684\u503c\u5c0f\u4e8e\u6839\u8282\u70b9\u7684\u503c\uff0c\u53f3\u5b50\u6811\u7684\u503c\u5927\u4e8e\u6839\u8282\u70b9\u7684\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u63d2\u5165\u8282\u70b9\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def insert(root, value):\n    if root is None:\n        return TreeNode(value)\n    if value &lt; root.value:\n        root.left = insert(root.left, value)\n    else:\n        root.right = insert(root.right, value)\n    return root\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4e8c\u53c9\u6811\u4e2d\u9002\u5f53\u5730\u63d2\u5165\u65b0\u8282\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001Python\u5982\u4f55\u8868\u793a\u4e8c\u53c9\u6811 \u5728Python\u4e2d\uff0c\u8868\u793a\u4e8c\u53c9\u6811\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528\u5b57\u5178\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1083088,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083084"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1083084"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083084\/revisions"}],"predecessor-version":[{"id":1083091,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083084\/revisions\/1083091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1083088"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1083084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1083084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1083084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}