{"id":1113448,"date":"2025-01-08T17:47:57","date_gmt":"2025-01-08T09:47:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113448.html"},"modified":"2025-01-08T17:48:02","modified_gmt":"2025-01-08T09:48:02","slug":"python%e4%ba%8c%e5%8f%89%e6%a0%91%e5%a6%82%e4%bd%95%e8%bd%ac%e4%b8%ba%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1113448.html","title":{"rendered":"python\u4e8c\u53c9\u6811\u5982\u4f55\u8f6c\u4e3a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075037\/9dabbaa1-5652-4b88-a518-60f4a11b5afe.webp\" alt=\"python\u4e8c\u53c9\u6811\u5982\u4f55\u8f6c\u4e3a\u5217\u8868\" \/><\/p>\n<p><p> \u5c06\u4e8c\u53c9\u6811\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u8fc7\u7a0b\u4e3b\u8981\u6d89\u53ca\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u6839\u636e\u904d\u5386\u987a\u5e8f\u5c06\u8282\u70b9\u7684\u503c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<strong>\u4e8c\u53c9\u6811\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u548c\u5c42\u6b21\u904d\u5386<\/strong>\uff0c\u5176\u4e2d\u5c42\u6b21\u904d\u5386\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5c42\u6b21\u904d\u5386\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5c42\u6b21\u904d\u5386\u662f\u6309\u7167\u6811\u7684\u5c42\u6b21\u4ece\u4e0a\u5230\u4e0b\u3001\u4ece\u5de6\u5230\u53f3\u4f9d\u6b21\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\uff0c\u5e76\u5c06\u8282\u70b9\u7684\u503c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/strong>\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5c06\u4e8c\u53c9\u6811\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u4fdd\u6301\u6811\u7684\u7ed3\u6784\uff0c\u5e76\u4e14\u6bcf\u4e2a\u8282\u70b9\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u4e0e\u6811\u4e2d\u7684\u4f4d\u7f6e\u5bf9\u5e94\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, value=0, left=None, right=None):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.left = left<\/p>\n<p>        self.right = right<\/p>\n<p>def tree_to_list(root):<\/p>\n<p>    if not root:<\/p>\n<p>        return []<\/p>\n<p>    result = []<\/p>\n<p>    queue = [root]<\/p>\n<p>    while queue:<\/p>\n<p>        current_node = queue.pop(0)<\/p>\n<p>        if current_node:<\/p>\n<p>            result.append(current_node.value)<\/p>\n<p>            queue.append(current_node.left)<\/p>\n<p>            queue.append(current_node.right)<\/p>\n<p>        else:<\/p>\n<p>            result.append(None)<\/p>\n<p>    # Remove tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ling None values<\/p>\n<p>    while result and result[-1] is None:<\/p>\n<p>        result.pop()<\/p>\n<p>    return result<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>root = TreeNode(1)<\/p>\n<p>root.left = TreeNode(2)<\/p>\n<p>root.right = TreeNode(3)<\/p>\n<p>root.left.left = TreeNode(4)<\/p>\n<p>root.left.right = TreeNode(5)<\/p>\n<p>print(tree_to_list(root))  # Output: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u524d\u5e8f\u904d\u5386\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u524d\u5e8f\u904d\u5386\u662f\u6307\u5148\u8bbf\u95ee\u6839\u8282\u70b9\uff0c\u7136\u540e\u9012\u5f52\u5730\u8bbf\u95ee\u5de6\u5b50\u6811\uff0c\u6700\u540e\u9012\u5f52\u5730\u8bbf\u95ee\u53f3\u5b50\u6811\u3002\u5728\u8f6c\u6362\u4e3a\u5217\u8868\u65f6\uff0c\u6309\u7167\u8fd9\u4e2a\u987a\u5e8f\u5c06\u8282\u70b9\u7684\u503c\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def preorder_traversal(root):<\/p>\n<p>    if not root:<\/p>\n<p>        return []<\/p>\n<p>    result = [root.value]<\/p>\n<p>    result.extend(preorder_traversal(root.left))<\/p>\n<p>    result.extend(preorder_traversal(root.right))<\/p>\n<p>    return result<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>print(preorder_traversal(root))  # Output: [1, 2, 4, 5, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4e2d\u5e8f\u904d\u5386\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4e2d\u5e8f\u904d\u5386\u662f\u6307\u5148\u9012\u5f52\u5730\u8bbf\u95ee\u5de6\u5b50\u6811\uff0c\u7136\u540e\u8bbf\u95ee\u6839\u8282\u70b9\uff0c\u6700\u540e\u9012\u5f52\u5730\u8bbf\u95ee\u53f3\u5b50\u6811\u3002\u5728\u8f6c\u6362\u4e3a\u5217\u8868\u65f6\uff0c\u6309\u7167\u8fd9\u4e2a\u987a\u5e8f\u5c06\u8282\u70b9\u7684\u503c\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def inorder_traversal(root):<\/p>\n<p>    if not root:<\/p>\n<p>        return []<\/p>\n<p>    result = []<\/p>\n<p>    result.extend(inorder_traversal(root.left))<\/p>\n<p>    result.append(root.value)<\/p>\n<p>    result.extend(inorder_traversal(root.right))<\/p>\n<p>    return result<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>print(inorder_traversal(root))  # Output: [4, 2, 5, 1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u540e\u5e8f\u904d\u5386\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u540e\u5e8f\u904d\u5386\u662f\u6307\u5148\u9012\u5f52\u5730\u8bbf\u95ee\u5de6\u5b50\u6811\uff0c\u7136\u540e\u9012\u5f52\u5730\u8bbf\u95ee\u53f3\u5b50\u6811\uff0c\u6700\u540e\u8bbf\u95ee\u6839\u8282\u70b9\u3002\u5728\u8f6c\u6362\u4e3a\u5217\u8868\u65f6\uff0c\u6309\u7167\u8fd9\u4e2a\u987a\u5e8f\u5c06\u8282\u70b9\u7684\u503c\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def postorder_traversal(root):<\/p>\n<p>    if not root:<\/p>\n<p>        return []<\/p>\n<p>    result = []<\/p>\n<p>    result.extend(postorder_traversal(root.left))<\/p>\n<p>    result.extend(postorder_traversal(root.right))<\/p>\n<p>    result.append(root.value)<\/p>\n<p>    return result<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>print(postorder_traversal(root))  # Output: [4, 5, 2, 3, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c42\u6b21\u904d\u5386\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5c42\u6b21\u904d\u5386\u4f7f\u7528\u961f\u5217\u6765\u5b9e\u73b0\uff0c\u9996\u5148\u5c06\u6839\u8282\u70b9\u5165\u961f\uff0c\u7136\u540e\u91cd\u590d\u4ee5\u4e0b\u6b65\u9aa4\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\uff1a\u5c06\u961f\u9996\u8282\u70b9\u51fa\u961f\uff0c\u5c06\u5176\u503c\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f9d\u6b21\u5c06\u5176\u5de6\u5b69\u5b50\u548c\u53f3\u5b69\u5b50\u5165\u961f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def level_order_traversal(root):<\/p>\n<p>    if not root:<\/p>\n<p>        return []<\/p>\n<p>    result = []<\/p>\n<p>    queue = [root]<\/p>\n<p>    while queue:<\/p>\n<p>        current_node = queue.pop(0)<\/p>\n<p>        if current_node:<\/p>\n<p>            result.append(current_node.value)<\/p>\n<p>            queue.append(current_node.left)<\/p>\n<p>            queue.append(current_node.right)<\/p>\n<p>        else:<\/p>\n<p>            result.append(None)<\/p>\n<p>    # Remove trailing None values<\/p>\n<p>    while result and result[-1] is None:<\/p>\n<p>        result.pop()<\/p>\n<p>    return result<\/p>\n<h2><strong>Example usage:<\/strong><\/h2>\n<p>print(level_order_traversal(root))  # Output: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5c06\u4e8c\u53c9\u6811\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u6709\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u548c\u5c42\u6b21\u904d\u5386<\/strong>\u3002\u5728\u5177\u4f53\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u904d\u5386\u65b9\u5f0f\u3002\u5c42\u6b21\u904d\u5386\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u4fdd\u6301\u6811\u7684\u7ed3\u6784\uff0c\u5e76\u4e14\u6bcf\u4e2a\u8282\u70b9\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u4e0e\u6811\u4e2d\u7684\u4f4d\u7f6e\u5bf9\u5e94\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u5176\u4ed6\u904d\u5386\u65b9\u5f0f\u4e5f\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u8fd9\u4e9b\u904d\u5386\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u548c\u64cd\u4f5c\u4e8c\u53c9\u6811\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u4e8c\u53c9\u6811\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08DFS\uff09\u6216\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\u7684\u65b9\u6cd5\u5c06\u4e8c\u53c9\u6811\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u6216\u961f\u5217\u6765\u5b9e\u73b0DFS\u548cBFS\u3002DFS\u4f1a\u751f\u6210\u524d\u5e8f\u3001\u4e2d\u5e8f\u6216\u540e\u5e8f\u904d\u5386\u7684\u5217\u8868\uff0c\u800cBFS\u5219\u4f1a\u751f\u6210\u5c42\u5e8f\u904d\u5386\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u4f7f\u7528\u9012\u5f52\u5c06\u4e8c\u53c9\u6811\u8f6c\u4e3a\u5217\u8868\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u786e\u4fdd\u5904\u7406\u6bcf\u4e2a\u8282\u70b9\uff0c\u5e76\u5c06\u8282\u70b9\u7684\u503c\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u6839\u636e\u4e0d\u540c\u7684\u904d\u5386\u65b9\u5f0f\uff08\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\uff09\uff0c\u8282\u70b9\u7684\u52a0\u5165\u987a\u5e8f\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u786e\u4fdd\u5728\u9012\u5f52\u8c03\u7528\u65f6\u6b63\u786e\u8bbf\u95ee\u5de6\u53f3\u5b50\u8282\u70b9\uff0c\u4ee5\u83b7\u5f97\u9884\u671f\u7684\u5217\u8868\u7ed3\u6784\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5185\u7f6e\u5e93\u6216\u5de5\u5177\u6765\u7b80\u5316\u4e8c\u53c9\u6811\u5230\u5217\u8868\u7684\u8f6c\u6362\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93\u4e2d\u6ca1\u6709\u4e13\u95e8\u5904\u7406\u4e8c\u53c9\u6811\u7684\u5de5\u5177\uff0c\u4f46\u53ef\u4ee5\u5229\u7528<code>collections.deque<\/code>\u6765\u5b9e\u73b0BFS\uff0c\u6216\u8005\u901a\u8fc7\u7f16\u5199\u7c7b\u548c\u65b9\u6cd5\u6765\u5c01\u88c5\u4e8c\u53c9\u6811\u7684\u64cd\u4f5c\u3002\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u8fd8\u4f1a\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>binarytree<\/code>\uff0c\u6765\u7b80\u5316\u6811\u7684\u521b\u5efa\u548c\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06\u4e8c\u53c9\u6811\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u8fc7\u7a0b\u4e3b\u8981\u6d89\u53ca\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u6839\u636e\u904d\u5386\u987a\u5e8f\u5c06\u8282\u70b9\u7684\u503c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002\u4e8c\u53c9\u6811\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd [&hellip;]","protected":false},"author":3,"featured_media":1113456,"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\/1113448"}],"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=1113448"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113448\/revisions"}],"predecessor-version":[{"id":1113458,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113448\/revisions\/1113458"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113456"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}