{"id":48042,"date":"2025-07-30T10:32:19","date_gmt":"2025-07-30T02:32:19","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/48042.html"},"modified":"2025-07-30T10:32:19","modified_gmt":"2025-07-30T02:32:19","slug":"python%e6%a0%91%e3%80%81%e4%ba%8c%e5%8f%89%e6%a0%91%e3%80%81%e4%ba%8c%e5%8f%89%e6%90%9c%e7%b4%a2%e6%a0%91%ef%bc%88bst%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/48042.html","title":{"rendered":"Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09"},"content":{"rendered":"<h2>\u4e00\u3001\u6811&#xff08;Tree&#xff09;<\/h2>\n<p>\u57fa\u672c\u6982\u5ff5<\/p>\n<p>\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784&#xff0c;\u7531\u8282\u70b9\u548c\u8fb9\u7ec4\u6210&#xff0c;\u5177\u6709\u4ee5\u4e0b\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6bcf\u4e2a\u8282\u70b9\u6709\u96f6\u4e2a\u6216\u591a\u4e2a\u5b50\u8282\u70b9<\/p>\n<\/li>\n<li>\n<p>\u6ca1\u6709\u7236\u8282\u70b9\u7684\u8282\u70b9\u79f0\u4e3a\u6839\u8282\u70b9<\/p>\n<\/li>\n<li>\n<p>\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\u79f0\u4e3a\u53f6\u8282\u70b9<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u4e2a\u975e\u6839\u8282\u70b9\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7236\u8282\u70b9<\/p>\n<\/li>\n<\/ul>\n<p>\u6811\u7684\u672f\u8bed<\/p>\n<ul>\n<li>\n<p>\u5ea6(Degree): \u8282\u70b9\u7684\u5b50\u6811\u4e2a\u6570<\/p>\n<\/li>\n<li>\n<p>\u6df1\u5ea6(Depth): \u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u7684\u8def\u5f84\u957f\u5ea6<\/p>\n<\/li>\n<li>\n<p>\u9ad8\u5ea6(Height): \u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u6700\u957f\u8def\u5f84<\/p>\n<\/li>\n<li>\n<p>\u5c42\u6b21(Level): \u6839\u8282\u70b9\u4e3a\u7b2c1\u5c42&#xff0c;\u4f9d\u6b21\u9012\u589e<\/p>\n<\/li>\n<\/ul>\n<p>\u666e\u901a\u6811\u7684Python\u5b9e\u73b0<\/p>\n<p>class TreeNode:<br \/>\n    &#034;&#034;&#034;\u6811\u8282\u70b9\u7c7b&#034;&#034;&#034;<br \/>\n    def __init__(self, data):<br \/>\n        self.data &#061; data          # \u8282\u70b9\u6570\u636e<br \/>\n        self.children &#061; []        # \u5b50\u8282\u70b9\u5217\u8868<\/p>\n<p>    def add_child(self, child_node):<br \/>\n        &#034;&#034;&#034;\u6dfb\u52a0\u5b50\u8282\u70b9&#034;&#034;&#034;<br \/>\n        self.children.append(child_node)<\/p>\n<p>    def __str__(self, level&#061;0):<br \/>\n        &#034;&#034;&#034;\u53ef\u89c6\u5316\u6811\u7ed3\u6784&#034;&#034;&#034;<br \/>\n        ret &#061; &#034;  &#034; * level &#043; str(self.data) &#043; &#034;\\\\n&#034; #\u5148\u5199\u597d\u6839\u8282\u70b9\u6570\u636e<br \/>\n        for child in self.children:<br \/>\n            ret &#043;&#061; child.__str__(level &#043; 1)<br \/>\n        return ret<\/p>\n<p># \u521b\u5efa\u6811<br \/>\nroot &#061; TreeNode(&#034;\u516c\u53f8&#034;)<br \/>\nsales &#061; TreeNode(&#034;\u9500\u552e\u90e8&#034;)<br \/>\nsales.add_child(TreeNode(&#034;\u9500\u552e\u4e00\u7ec4&#034;))<br \/>\nsales.add_child(TreeNode(&#034;\u9500\u552e\u4e8c\u7ec4&#034;))<\/p>\n<p>tech &#061; TreeNode(&#034;\u6280\u672f\u90e8&#034;)<br \/>\ntech.add_child(TreeNode(&#034;\u5f00\u53d1\u7ec4&#034;))<br \/>\ntech.add_child(TreeNode(&#034;\u6d4b\u8bd5\u7ec4&#034;))<\/p>\n<p>root.add_child(sales)<br \/>\nroot.add_child(tech)<\/p>\n<p>print(root)<br \/>\n# \u8f93\u51fa:<br \/>\n# \u516c\u53f8<br \/>\n#   \u9500\u552e\u90e8<br \/>\n#     \u9500\u552e\u4e00\u7ec4<br \/>\n#     \u9500\u552e\u4e8c\u7ec4<br \/>\n#   \u6280\u672f\u90e8<br \/>\n#     \u5f00\u53d1\u7ec4<br \/>\n#     \u6d4b\u8bd5\u7ec4<\/p>\n<h2>\u4e8c\u3001\u4e8c\u53c9\u6811&#xff08;Binary Tree&#xff09;<\/h2>\n<p>\u57fa\u672c\u6982\u5ff5<\/p>\n<p>\u4e8c\u53c9\u6811\u662f\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u6811\u7ed3\u6784&#xff0c;\u79f0\u4e3a&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5de6\u5b50\u8282\u70b9<\/p>\n<\/li>\n<li>\n<p>\u53f3\u5b50\u8282\u70b9<\/p>\n<\/li>\n<\/ul>\n<p>\u4e8c\u53c9\u6811\u7684\u6027\u8d28<\/p>\n<li>\n<p>\u7b2c<img decoding=\"async\" alt=\"i\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-6889843221b65.png\" \/>\u5c42\u6700\u591a\u6709<img decoding=\"async\" alt=\"2^{i-1}\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-68898432408f9.png\" \/>\u4e2a\u8282\u70b9<\/p>\n<\/li>\n<li>\n<p>\u6df1\u5ea6\u4e3a<img decoding=\"async\" alt=\"k\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-68898432487f2.png\" \/>\u7684\u4e8c\u53c9\u6811\u6700\u591a\u6709<img decoding=\"async\" alt=\"2^k-1\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-688984324fe8f.png\" \/>\u4e2a\u8282\u70b9<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e\u4efb\u4f55\u975e\u7a7a\u4e8c\u53c9\u6811&#xff0c;<img decoding=\"async\" alt=\"n_0 &#061; n_2&#043;1\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-6889843259536.png\" \/>&#xff08;<img decoding=\"async\" alt=\"n_0\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-6889843264c8a.png\" \/>&#xff1a;\u53f6\u8282\u70b9\u6570&#xff0c;<img decoding=\"async\" alt=\"n_2\" class=\"mathcode\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-688984326ce88.png\" \/>&#xff1a;\u5ea6\u4e3a2\u7684\u8282\u70b9\u6570&#xff09;<\/p>\n<\/li>\n<p>\u00a0Python\u5b9e\u73b0\u4e8c\u53c9\u6811<\/p>\n<p>class BinaryTreeNode:<br \/>\n    &#034;&#034;&#034;\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, value):<br \/>\n        self.value &#061; value  # \u8282\u70b9\u503c<br \/>\n        self.left &#061; None  # \u5de6\u5b50\u8282\u70b9<br \/>\n        self.right &#061; None  # \u53f3\u5b50\u8282\u70b9<\/p>\n<p>    def __str__(self):<br \/>\n        &#034;&#034;&#034;\u8fd4\u56de\u6a2a\u8eba\u5b57\u7b26\u753b\u5b57\u7b26\u4e32&#034;&#034;&#034;<br \/>\n        lines &#061; []<br \/>\n        self._build_lines(self, &#034;&#034;, True, lines)<br \/>\n        return &#034;\\\\n&#034;.join(lines)<\/p>\n<p>    # \u5185\u90e8\u9012\u5f52&#xff0c;\u628a\u6bcf\u4e00\u884c append \u5230 lines \u5217\u8868<br \/>\n    def _build_lines(self, node, prefix, is_left, lines):<br \/>\n        if node is None:<br \/>\n            return<br \/>\n        self._build_lines(node.right,<br \/>\n                          prefix &#043; (&#034;\u2502   &#034; if is_left else &#034;    &#034;), False, lines)<br \/>\n        connector &#061; &#034;\u2514\u2500\u2500 &#034; if is_left else &#034;\u250c\u2500\u2500 &#034;<br \/>\n        if prefix &#061;&#061; &#034;&#034;:<br \/>\n            connector &#061; &#034;&#034;<br \/>\n        lines.append(prefix &#043; connector &#043; str(node.value))<br \/>\n        self._build_lines(node.left,<br \/>\n                          prefix &#043; (&#034;    &#034; if is_left else &#034;\u2502   &#034;), True, lines)<\/p>\n<p># \u521b\u5efa\u4e8c\u53c9\u6811<br \/>\nroot &#061; BinaryTreeNode(1)<br \/>\nroot.left &#061; BinaryTreeNode(2)<br \/>\nroot.right &#061; BinaryTreeNode(3)<br \/>\nroot.left.left &#061; BinaryTreeNode(4)<br \/>\nroot.left.right &#061; BinaryTreeNode(5)<\/p>\n<p>print(root)<br \/>\n# \u8f93\u51fa:<br \/>\n# \u2502   \u250c\u2500\u2500 3<br \/>\n# 1<br \/>\n#     \u2502   \u250c\u2500\u2500 5<br \/>\n#     \u2514\u2500\u2500 2<br \/>\n#         \u2514\u2500\u2500 4<\/p>\n<p>\u4e8c\u53c9\u6811\u904d\u5386\u65b9\u5f0f<\/p>\n<ul>\n<li>\u524d\u5e8f\u904d\u5386(Pre-order): \u6839 -&gt; \u5de6 -&gt; \u53f3<\/li>\n<li>\u4e2d\u5e8f\u904d\u5386(In-order): \u5de6 -&gt; \u6839 -&gt; \u53f3<\/li>\n<li>\u540e\u5e8f\u904d\u5386(Post-order): \u5de6 -&gt; \u53f3 -&gt; \u6839<\/li>\n<li>\u5c42\u5e8f\u904d\u5386(Level-order): \u6309\u5c42\u6b21\u4ece\u4e0a\u5230\u4e0b\u3001\u4ece\u5de6\u5230\u53f3<\/li>\n<\/ul>\n<p>def preorder_traversal(node):<br \/>\n    &#034;&#034;&#034;\u524d\u5e8f\u904d\u5386&#034;&#034;&#034;<br \/>\n    if node:<br \/>\n        print(node.value, end&#061;&#034; &#034;)<br \/>\n        preorder_traversal(node.left)<br \/>\n        preorder_traversal(node.right)<\/p>\n<p>def inorder_traversal(node):<br \/>\n    &#034;&#034;&#034;\u4e2d\u5e8f\u904d\u5386&#034;&#034;&#034;<br \/>\n    if node:<br \/>\n        inorder_traversal(node.left)<br \/>\n        print(node.value, end&#061;&#034; &#034;)<br \/>\n        inorder_traversal(node.right)<\/p>\n<p>def postorder_traversal(node):<br \/>\n    &#034;&#034;&#034;\u540e\u5e8f\u904d\u5386&#034;&#034;&#034;<br \/>\n    if node:<br \/>\n        postorder_traversal(node.left)<br \/>\n        postorder_traversal(node.right)<br \/>\n        print(node.value, end&#061;&#034; &#034;)<\/p>\n<p>def levelorder_traversal(node):<br \/>\n    &#034;&#034;&#034;\u5c42\u5e8f\u904d\u5386&#034;&#034;&#034;<br \/>\n    if not node:<br \/>\n        return<br \/>\n    queue &#061; [node]<br \/>\n    while queue:<br \/>\n        current &#061; queue.pop(0)<br \/>\n        print(current.value, end&#061;&#034; &#034;)<br \/>\n        if current.left:<br \/>\n            queue.append(current.left)<br \/>\n        if current.right:<br \/>\n            queue.append(current.right)<\/p>\n<p># \u6d4b\u8bd5\u904d\u5386<br \/>\nprint(&#034;\\\\n\u524d\u5e8f\u904d\u5386:&#034;, end&#061;&#034; &#034;)<br \/>\npreorder_traversal(root)  # \u8f93\u51fa: 1 2 4 5 3<\/p>\n<p>print(&#034;\\\\n\u4e2d\u5e8f\u904d\u5386:&#034;, end&#061;&#034; &#034;)<br \/>\ninorder_traversal(root)   # \u8f93\u51fa: 4 2 5 1 3<\/p>\n<p>print(&#034;\\\\n\u540e\u5e8f\u904d\u5386:&#034;, end&#061;&#034; &#034;)<br \/>\npostorder_traversal(root) # \u8f93\u51fa: 4 5 2 3 1<\/p>\n<p>print(&#034;\\\\n\u5c42\u5e8f\u904d\u5386:&#034;, end&#061;&#034; &#034;)<br \/>\nlevelorder_traversal(root) # \u8f93\u51fa: 1 2 3 4 5<\/p>\n<h2>\u4e09\u3001\u4e8c\u53c9\u641c\u7d22\u6811&#xff08;BST-Binary Search Tree&#xff09;<\/h2>\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u4e8c\u53c9\u6811&#xff0c;\u6ee1\u8db3&#xff1a;<\/p>\n<ul>\n<li>\u5de6\u5b50\u6811\u6240\u6709\u8282\u70b9\u7684\u503c &lt; \u6839\u8282\u70b9\u7684\u503c<\/li>\n<li>\u53f3\u5b50\u6811\u6240\u6709\u8282\u70b9\u7684\u503c &gt; \u6839\u8282\u70b9\u7684\u503c<\/li>\n<li>\u5de6\u53f3\u5b50\u6811\u4e5f\u5206\u522b\u662fBST<\/li>\n<\/ul>\n<p>BST\u7684\u6027\u8d28<\/p>\n<ul>\n<li>\u4e2d\u5e8f\u904d\u5386BST\u4f1a\u5f97\u5230\u4e00\u4e2a\u5347\u5e8f\u5e8f\u5217<\/li>\n<li>\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(log n)&#xff0c;\u6700\u574fO(n)&#xff08;\u9000\u5316\u4e3a\u94fe\u8868\u65f6&#xff09;<\/li>\n<\/ul>\n<p>Python\u5b9e\u73b0BST<\/p>\n<p>class BSTNode:<br \/>\n    &#034;&#034;&#034;\u4e8c\u53c9\u641c\u7d22\u6811\u8282\u70b9\u7c7b&#034;&#034;&#034;<\/p>\n<p>    def __init__(self, key):<br \/>\n        self.key &#061; key  # \u8282\u70b9\u952e\u503c<br \/>\n        self.left &#061; None  # \u5de6\u5b50\u8282\u70b9<br \/>\n        self.right &#061; None  # \u53f3\u5b50\u8282\u70b9<br \/>\n        self.parent &#061; None  # \u7236\u8282\u70b9&#xff08;\u53ef\u9009&#xff09;<\/p>\n<p>    def __str__(self):<br \/>\n        &#034;&#034;&#034;\u53ef\u89c6\u5316BST\u7ed3\u6784&#034;&#034;&#034;<br \/>\n        return f&#034;{self.key}(L:{self.left}, R:{self.right})&#034;<\/p>\n<p>class BinarySearchTree:<br \/>\n    &#034;&#034;&#034;\u4e8c\u53c9\u641c\u7d22\u6811\u7c7b&#034;&#034;&#034;<\/p>\n<p>    def __init__(self):<br \/>\n        self.root &#061; None<\/p>\n<p>    def insert(self, key):<br \/>\n        &#034;&#034;&#034;\u63d2\u5165\u8282\u70b9&#034;&#034;&#034;<br \/>\n        if self.root is None:<br \/>\n            self.root &#061; BSTNode(key)<br \/>\n        else:<br \/>\n            self._insert_recursive(self.root, key)<\/p>\n<p>    def _insert_recursive(self, node, key):<br \/>\n        &#034;&#034;&#034;\u9012\u5f52\u63d2\u5165\u8f85\u52a9\u51fd\u6570&#034;&#034;&#034;<br \/>\n        if key &lt; node.key:<br \/>\n            if node.left is None:<br \/>\n                node.left &#061; BSTNode(key)<br \/>\n                node.left.parent &#061; node<br \/>\n            else:<br \/>\n                self._insert_recursive(node.left, key)<br \/>\n        elif key &gt; node.key:<br \/>\n            if node.right is None:<br \/>\n                node.right &#061; BSTNode(key)<br \/>\n                node.right.parent &#061; node<br \/>\n            else:<br \/>\n                self._insert_recursive(node.right, key)<br \/>\n        # \u5982\u679ckey &#061;&#061; node.key&#xff0c;\u53ef\u4ee5\u9009\u62e9\u5ffd\u7565\u6216\u5904\u7406\u91cd\u590d\u503c<br \/>\n        elif key &#061;&#061; node.key:<br \/>\n            # \u7b56\u7565 1&#xff1a;\u76f4\u63a5\u5ffd\u7565&#xff08;\u6700\u7b80\u5355&#xff09;<br \/>\n            return<\/p>\n<p>            # \u7b56\u7565 2&#xff1a;\u8ba1\u6570\u6cd5&#xff08;\u7ed9\u8282\u70b9\u52a0\u4e00\u4e2a count \u5c5e\u6027&#xff09;<br \/>\n            # node.count &#043;&#061; 1<\/p>\n<p>    def search(self, key):<br \/>\n        &#034;&#034;&#034;\u67e5\u627e\u8282\u70b9&#034;&#034;&#034;<br \/>\n        return self._search_recursive(self.root, key)<\/p>\n<p>    def _search_recursive(self, node, key):<br \/>\n        &#034;&#034;&#034;\u9012\u5f52\u67e5\u627e\u8f85\u52a9\u51fd\u6570&#034;&#034;&#034;<br \/>\n        if node is None or node.key &#061;&#061; key:<br \/>\n            return node<br \/>\n        if key &lt; node.key:<br \/>\n            return self._search_recursive(node.left, key)<br \/>\n        return self._search_recursive(node.right, key)<\/p>\n<p>    def delete(self, key):<br \/>\n        &#034;&#034;&#034;\u5220\u9664\u8282\u70b9&#034;&#034;&#034;<br \/>\n        self.root &#061; self._delete_recursive(self.root, key)<\/p>\n<p>    def _delete_recursive(self, node, key):<br \/>\n        &#034;&#034;&#034;\u9012\u5f52\u5220\u9664\u8f85\u52a9\u51fd\u6570&#034;&#034;&#034;<br \/>\n        if node is None:<br \/>\n            return node<\/p>\n<p>        if key &lt; node.key:<br \/>\n            node.left &#061; self._delete_recursive(node.left, key)<br \/>\n        elif key &gt; node.key:<br \/>\n            node.right &#061; self._delete_recursive(node.right, key)<br \/>\n        else:<br \/>\n            # \u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9<br \/>\n            if node.left is None: #\u56ca\u62ec\u4e86\u5de6\u53f3\u5b50\u6811\u5747\u4e3a\u7a7a\u7684\u60c5\u51b5\u3002<br \/>\n                return node.right<br \/>\n            elif node.right is None:<br \/>\n                return node.left<br \/>\n            else:<br \/>\n                # \u6709\u4e24\u4e2a\u5b50\u8282\u70b9&#xff0c;\u627e\u5230\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u66ff\u4ee3&#xff0c;\u56e0\u4e3a\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u503c\u4e00\u5b9a\u6bd4\u5de6\u5b50\u6811\u5927&#xff0c;\u4f46\u53c8\u6bd4\u53f3\u5b50\u6811\u5c0f<br \/>\n                successor &#061; self._find_min(node.right)<br \/>\n                node.key &#061; successor.key<br \/>\n                node.right &#061; self._delete_recursive(node.right, successor.key)<br \/>\n        return node<\/p>\n<p>    def _find_min(self, node):<br \/>\n        &#034;&#034;&#034;\u627e\u5230\u5b50\u6811\u4e2d\u7684\u6700\u5c0f\u8282\u70b9&#034;&#034;&#034;<br \/>\n        current &#061; node<br \/>\n        while current.left is not None:<br \/>\n            current &#061; current.left<br \/>\n        return current<\/p>\n<p>    def inorder_traversal(self):<br \/>\n        &#034;&#034;&#034;\u4e2d\u5e8f\u904d\u5386&#xff08;\u8fd4\u56de\u6709\u5e8f\u5e8f\u5217&#xff09;&#034;&#034;&#034;<br \/>\n        result &#061; []<br \/>\n        self._inorder_recursive(self.root, result)<br \/>\n        return result<\/p>\n<p>    def _inorder_recursive(self, node, result):<br \/>\n        &#034;&#034;&#034;\u4e2d\u5e8f\u904d\u5386\u8f85\u52a9\u51fd\u6570&#034;&#034;&#034;<br \/>\n        if node:<br \/>\n            self._inorder_recursive(node.left, result)<br \/>\n            result.append(node.key)<br \/>\n            self._inorder_recursive(node.right, result)<\/p>\n<p># \u6d4b\u8bd5BST<br \/>\nbst &#061; BinarySearchTree()<br \/>\nkeys &#061; [50, 30, 70, 20, 40, 60, 80]<\/p>\n<p>for key in keys:<br \/>\n    bst.insert(key)<\/p>\n<p>print(&#034;\u4e2d\u5e8f\u904d\u5386:&#034;, bst.inorder_traversal())  # \u8f93\u51fa: [20, 30, 40, 50, 60, 70, 80]<\/p>\n<p># \u67e5\u627e\u6d4b\u8bd5<br \/>\nprint(&#034;\u67e5\u627e40:&#034;, bst.search(40) is not None)  # \u8f93\u51fa: True<br \/>\nprint(&#034;\u67e5\u627e45:&#034;, bst.search(45) is not None)  # \u8f93\u51fa: False<\/p>\n<p># \u5220\u9664\u6d4b\u8bd5<br \/>\nbst.delete(30)<br \/>\nprint(&#034;\u5220\u966430\u540e\u4e2d\u5e8f\u904d\u5386:&#034;, bst.inorder_traversal())  # \u8f93\u51fa: [20, 40, 50, 60, 70, 80]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb243\u6b21\u3002\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7531\u8282\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a\u6bcf\u4e2a\u8282\u70b9\u6709\u96f6\u4e2a\u6216\u591a\u4e2a\u5b50\u8282\u70b9\u6ca1\u6709\u7236\u8282\u70b9\u7684\u8282\u70b9\u79f0\u4e3a\u6839\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\u79f0\u4e3a\u53f6\u8282\u70b9\u6bcf\u4e2a\u975e\u6839\u8282\u70b9\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7236\u8282\u70b9\u5ea6(Degree): \u8282\u70b9\u7684\u5b50\u6811\u4e2a\u6570\u6df1\u5ea6(Depth): \u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u7684\u8def\u5f84\u957f\u5ea6\u9ad8\u5ea6(Height): \u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u6700\u957f\u8def\u5f84\u5c42\u6b21(Level): \u6839\u8282\u70b9\u4e3a\u7b2c1\u5c42\uff0c\u4f9d\u6b21\u9012\u589e\u3002<\/p>\n","protected":false},"author":2,"featured_media":48035,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,4631,190],"topic":[],"class_list":{"0":"post-48042","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-server","8":"tag-python","10":"tag-190"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/48042.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb243\u6b21\u3002\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7531\u8282\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a\u6bcf\u4e2a\u8282\u70b9\u6709\u96f6\u4e2a\u6216\u591a\u4e2a\u5b50\u8282\u70b9\u6ca1\u6709\u7236\u8282\u70b9\u7684\u8282\u70b9\u79f0\u4e3a\u6839\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\u79f0\u4e3a\u53f6\u8282\u70b9\u6bcf\u4e2a\u975e\u6839\u8282\u70b9\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7236\u8282\u70b9\u5ea6(Degree): \u8282\u70b9\u7684\u5b50\u6811\u4e2a\u6570\u6df1\u5ea6(Depth): \u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u7684\u8def\u5f84\u957f\u5ea6\u9ad8\u5ea6(Height): \u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u6700\u957f\u8def\u5f84\u5c42\u6b21(Level): \u6839\u8282\u70b9\u4e3a\u7b2c1\u5c42\uff0c\u4f9d\u6b21\u9012\u589e\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/48042.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T02:32:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-6889843221b65.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/48042.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/48042.html\",\"name\":\"Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T02:32:19+00:00\",\"dateModified\":\"2025-07-30T02:32:19+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/48042.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/48042.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/48042.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/48042.html","og_locale":"zh_CN","og_type":"article","og_title":"Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb243\u6b21\u3002\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7531\u8282\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a\u6bcf\u4e2a\u8282\u70b9\u6709\u96f6\u4e2a\u6216\u591a\u4e2a\u5b50\u8282\u70b9\u6ca1\u6709\u7236\u8282\u70b9\u7684\u8282\u70b9\u79f0\u4e3a\u6839\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\u79f0\u4e3a\u53f6\u8282\u70b9\u6bcf\u4e2a\u975e\u6839\u8282\u70b9\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7236\u8282\u70b9\u5ea6(Degree): \u8282\u70b9\u7684\u5b50\u6811\u4e2a\u6570\u6df1\u5ea6(Depth): \u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u7684\u8def\u5f84\u957f\u5ea6\u9ad8\u5ea6(Height): \u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u6700\u957f\u8def\u5f84\u5c42\u6b21(Level): \u6839\u8282\u70b9\u4e3a\u7b2c1\u5c42\uff0c\u4f9d\u6b21\u9012\u589e\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/48042.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T02:32:19+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730023218-6889843221b65.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/48042.html","url":"https:\/\/www.wsisp.com\/helps\/48042.html","name":"Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T02:32:19+00:00","dateModified":"2025-07-30T02:32:19+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/48042.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/48042.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/48042.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python\u6811\u3001\u4e8c\u53c9\u6811\u3001\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/48042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=48042"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/48042\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/48035"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=48042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=48042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=48042"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=48042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}