mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Add the code to the docs.
This commit is contained in:
		@ -12,13 +12,14 @@ from include import *
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
""" Driver Code """
 | 
					""" Driver Code """
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    # 初始化二叉树
 | 
					    """ 初始化二叉树 """
 | 
				
			||||||
    # 初始化节点
 | 
					    # 初始化节点
 | 
				
			||||||
    n1 = TreeNode(val=1)
 | 
					    n1 = TreeNode(val=1)
 | 
				
			||||||
    n2 = TreeNode(val=2)
 | 
					    n2 = TreeNode(val=2)
 | 
				
			||||||
    n3 = TreeNode(val=3)
 | 
					    n3 = TreeNode(val=3)
 | 
				
			||||||
    n4 = TreeNode(val=4)
 | 
					    n4 = TreeNode(val=4)
 | 
				
			||||||
    n5 = TreeNode(val=5)
 | 
					    n5 = TreeNode(val=5)
 | 
				
			||||||
 | 
					    # 构建引用指向(即指针)
 | 
				
			||||||
    n1.left = n2
 | 
					    n1.left = n2
 | 
				
			||||||
    n1.right = n3
 | 
					    n1.right = n3
 | 
				
			||||||
    n2.left = n4
 | 
					    n2.left = n4
 | 
				
			||||||
@ -26,15 +27,13 @@ if __name__ == "__main__":
 | 
				
			|||||||
    print("\n初始化二叉树\n")
 | 
					    print("\n初始化二叉树\n")
 | 
				
			||||||
    print_tree(n1)
 | 
					    print_tree(n1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # 插入与删除结点
 | 
					    """ 插入与删除结点 """
 | 
				
			||||||
    P = TreeNode(0)
 | 
					    P = TreeNode(0)
 | 
				
			||||||
 | 
					 | 
				
			||||||
    # 在 n1 -> n2 中间插入节点 P
 | 
					    # 在 n1 -> n2 中间插入节点 P
 | 
				
			||||||
    n1.left = P
 | 
					    n1.left = P
 | 
				
			||||||
    P.left = n2
 | 
					    P.left = n2
 | 
				
			||||||
    print("\n插入结点 P 后\n")
 | 
					    print("\n插入结点 P 后\n")
 | 
				
			||||||
    print_tree(n1)
 | 
					    print_tree(n1)
 | 
				
			||||||
 | 
					 | 
				
			||||||
    # 删除结点
 | 
					    # 删除结点
 | 
				
			||||||
    n1.left = n2
 | 
					    n1.left = n2
 | 
				
			||||||
    print("\n删除结点 P 后\n");
 | 
					    print("\n删除结点 P 后\n");
 | 
				
			||||||
 | 
				
			|||||||
@ -14,22 +14,18 @@ from include import *
 | 
				
			|||||||
""" 层序遍历 """
 | 
					""" 层序遍历 """
 | 
				
			||||||
def hier_order(root: TreeNode):
 | 
					def hier_order(root: TreeNode):
 | 
				
			||||||
    # 初始化队列,加入根结点
 | 
					    # 初始化队列,加入根结点
 | 
				
			||||||
    queue: typing.Deque[TreeNode] = collections.deque()
 | 
					    queue = collections.deque()
 | 
				
			||||||
    queue.append(root)
 | 
					    queue.append(root)
 | 
				
			||||||
    # 初始化一个列表,用于保存遍历序列
 | 
					    # 初始化一个列表,用于保存遍历序列
 | 
				
			||||||
    result = []
 | 
					    res = []
 | 
				
			||||||
    while queue:
 | 
					    while queue:
 | 
				
			||||||
        # 队列出队
 | 
					        node = queue.popleft()       # 队列出队
 | 
				
			||||||
        node = queue.popleft()
 | 
					        res.append(node.val)         # 保存节点值
 | 
				
			||||||
        # 保存节点值
 | 
					 | 
				
			||||||
        result.append(node.val)
 | 
					 | 
				
			||||||
        if node.left is not None:
 | 
					        if node.left is not None:
 | 
				
			||||||
            # 左子结点入队
 | 
					            queue.append(node.left)  # 左子结点入队
 | 
				
			||||||
            queue.append(node.left)
 | 
					 | 
				
			||||||
        if node.right is not None:
 | 
					        if node.right is not None:
 | 
				
			||||||
            # 右子结点入队
 | 
					            queue.append(node.right) # 右子结点入队
 | 
				
			||||||
            queue.append(node.right)
 | 
					    return res
 | 
				
			||||||
    return result
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
""" Driver Code """
 | 
					""" Driver Code """
 | 
				
			||||||
@ -41,6 +37,6 @@ if __name__ == "__main__":
 | 
				
			|||||||
    print_tree(root)
 | 
					    print_tree(root)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # 层序遍历
 | 
					    # 层序遍历
 | 
				
			||||||
    result = hier_order(root)
 | 
					    res = hier_order(root)
 | 
				
			||||||
    print("\n层序遍历的结点打印序列 = ", result)
 | 
					    print("\n层序遍历的结点打印序列 = ", res)
 | 
				
			||||||
    assert result == [1, 2, 3, 4, 5, 6, 7]
 | 
					    assert res == [1, 2, 3, 4, 5, 6, 7]
 | 
				
			||||||
 | 
				
			|||||||
@ -11,40 +11,34 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
 | 
				
			|||||||
from include import *
 | 
					from include import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
result = []
 | 
					res = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					""" 前序遍历 """
 | 
				
			||||||
""" 前序遍历二叉树 """
 | 
					 | 
				
			||||||
def pre_order(root: typing.Optional[TreeNode]):
 | 
					def pre_order(root: typing.Optional[TreeNode]):
 | 
				
			||||||
    if root is None:
 | 
					    if root is None:
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					 | 
				
			||||||
    # 访问优先级:根结点 -> 左子树 -> 右子树
 | 
					    # 访问优先级:根结点 -> 左子树 -> 右子树
 | 
				
			||||||
    result.append(root.val)
 | 
					    res.append(root.val)
 | 
				
			||||||
    pre_order(root=root.left)
 | 
					    pre_order(root=root.left)
 | 
				
			||||||
    pre_order(root=root.right)
 | 
					    pre_order(root=root.right)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					""" 中序遍历 """
 | 
				
			||||||
""" 中序遍历二叉树 """
 | 
					 | 
				
			||||||
def in_order(root: typing.Optional[TreeNode]):
 | 
					def in_order(root: typing.Optional[TreeNode]):
 | 
				
			||||||
    if root is None:
 | 
					    if root is None:
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					 | 
				
			||||||
    # 访问优先级:左子树 -> 根结点 -> 右子树
 | 
					    # 访问优先级:左子树 -> 根结点 -> 右子树
 | 
				
			||||||
    in_order(root=root.left)
 | 
					    in_order(root=root.left)
 | 
				
			||||||
    result.append(root.val)
 | 
					    res.append(root.val)
 | 
				
			||||||
    in_order(root=root.right)
 | 
					    in_order(root=root.right)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					""" 后序遍历 """
 | 
				
			||||||
""" 后序遍历二叉树 """
 | 
					 | 
				
			||||||
def post_order(root: typing.Optional[TreeNode]):
 | 
					def post_order(root: typing.Optional[TreeNode]):
 | 
				
			||||||
    if root is None:
 | 
					    if root is None:
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					 | 
				
			||||||
    # 访问优先级:左子树 -> 右子树 -> 根结点
 | 
					    # 访问优先级:左子树 -> 右子树 -> 根结点
 | 
				
			||||||
    post_order(root=root.left)
 | 
					    post_order(root=root.left)
 | 
				
			||||||
    post_order(root=root.right)
 | 
					    post_order(root=root.right)
 | 
				
			||||||
    result.append(root.val)
 | 
					    res.append(root.val)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
""" Driver Code """
 | 
					""" Driver Code """
 | 
				
			||||||
@ -56,19 +50,19 @@ if __name__ == "__main__":
 | 
				
			|||||||
    print_tree(root)
 | 
					    print_tree(root)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # 前序遍历
 | 
					    # 前序遍历
 | 
				
			||||||
    result.clear()
 | 
					    res.clear()
 | 
				
			||||||
    pre_order(root)
 | 
					    pre_order(root)
 | 
				
			||||||
    print("\n前序遍历的结点打印序列 = ", result)
 | 
					    print("\n前序遍历的结点打印序列 = ", res)
 | 
				
			||||||
    assert result == [1, 2, 4, 5, 3, 6, 7]
 | 
					    assert res == [1, 2, 4, 5, 3, 6, 7]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # 中序遍历
 | 
					    # 中序遍历
 | 
				
			||||||
    result.clear()
 | 
					    res.clear()
 | 
				
			||||||
    in_order(root)
 | 
					    in_order(root)
 | 
				
			||||||
    print("\n中序遍历的结点打印序列 = ", result)
 | 
					    print("\n中序遍历的结点打印序列 = ", res)
 | 
				
			||||||
    assert result == [4, 2, 5, 1, 6, 3, 7]
 | 
					    assert res == [4, 2, 5, 1, 6, 3, 7]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # 后序遍历
 | 
					    # 后序遍历
 | 
				
			||||||
    result.clear()
 | 
					    res.clear()
 | 
				
			||||||
    post_order(root)
 | 
					    post_order(root)
 | 
				
			||||||
    print("\n后序遍历的结点打印序列 = ", result)
 | 
					    print("\n后序遍历的结点打印序列 = ", res)
 | 
				
			||||||
    assert result == [4, 5, 2, 6, 7, 3, 1]
 | 
					    assert res == [4, 5, 2, 6, 7, 3, 1]
 | 
				
			||||||
 | 
				
			|||||||
@ -182,7 +182,7 @@ comments: true
 | 
				
			|||||||
=== "Python"
 | 
					=== "Python"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```python title="binary_tree.py"
 | 
					    ```python title="binary_tree.py"
 | 
				
			||||||
    # 初始化二叉树
 | 
					    """ 初始化二叉树 """
 | 
				
			||||||
    # 初始化节点
 | 
					    # 初始化节点
 | 
				
			||||||
    n1 = TreeNode(val=1)
 | 
					    n1 = TreeNode(val=1)
 | 
				
			||||||
    n2 = TreeNode(val=2)
 | 
					    n2 = TreeNode(val=2)
 | 
				
			||||||
@ -302,7 +302,7 @@ comments: true
 | 
				
			|||||||
=== "Python"
 | 
					=== "Python"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```python title="binary_tree.py"
 | 
					    ```python title="binary_tree.py"
 | 
				
			||||||
    # 插入与删除结点
 | 
					    """ 插入与删除结点 """
 | 
				
			||||||
    p = TreeNode(0)
 | 
					    p = TreeNode(0)
 | 
				
			||||||
    # 在 n1 -> n2 中间插入结点 P
 | 
					    # 在 n1 -> n2 中间插入结点 P
 | 
				
			||||||
    n1.left = p
 | 
					    n1.left = p
 | 
				
			||||||
@ -461,7 +461,7 @@ comments: true
 | 
				
			|||||||
=== "Python"
 | 
					=== "Python"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```python title=""
 | 
					    ```python title=""
 | 
				
			||||||
    “”“ 二叉树的数组表示 ”“”
 | 
					    """ 二叉树的数组表示 """
 | 
				
			||||||
    # 直接使用 None 来表示空位
 | 
					    # 直接使用 None 来表示空位
 | 
				
			||||||
    tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
 | 
					    tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
				
			|||||||
@ -65,7 +65,21 @@ comments: true
 | 
				
			|||||||
=== "Python"
 | 
					=== "Python"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```python title="binary_tree_bfs.py"
 | 
					    ```python title="binary_tree_bfs.py"
 | 
				
			||||||
    
 | 
					    """ 层序遍历 """
 | 
				
			||||||
 | 
					    def hier_order(root: TreeNode):
 | 
				
			||||||
 | 
					        # 初始化队列,加入根结点
 | 
				
			||||||
 | 
					        queue = collections.deque()
 | 
				
			||||||
 | 
					        queue.append(root)
 | 
				
			||||||
 | 
					        # 初始化一个列表,用于保存遍历序列
 | 
				
			||||||
 | 
					        res = []
 | 
				
			||||||
 | 
					        while queue:
 | 
				
			||||||
 | 
					            node = queue.popleft()       # 队列出队
 | 
				
			||||||
 | 
					            res.append(node.val)         # 保存节点值
 | 
				
			||||||
 | 
					            if node.left is not None:
 | 
				
			||||||
 | 
					                queue.append(node.left)  # 左子结点入队
 | 
				
			||||||
 | 
					            if node.right is not None:
 | 
				
			||||||
 | 
					                queue.append(node.right) # 右子结点入队
 | 
				
			||||||
 | 
					        return res
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "Go"
 | 
					=== "Go"
 | 
				
			||||||
@ -256,7 +270,32 @@ comments: true
 | 
				
			|||||||
=== "Python"
 | 
					=== "Python"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```python title="binary_tree_dfs.py"
 | 
					    ```python title="binary_tree_dfs.py"
 | 
				
			||||||
 | 
					    """ 前序遍历 """
 | 
				
			||||||
 | 
					    def pre_order(root: typing.Optional[TreeNode]):
 | 
				
			||||||
 | 
					        if root is None:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        # 访问优先级:根结点 -> 左子树 -> 右子树
 | 
				
			||||||
 | 
					        res.append(root.val)
 | 
				
			||||||
 | 
					        pre_order(root=root.left)
 | 
				
			||||||
 | 
					        pre_order(root=root.right)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    """ 中序遍历 """
 | 
				
			||||||
 | 
					    def in_order(root: typing.Optional[TreeNode]):
 | 
				
			||||||
 | 
					        if root is None:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        # 访问优先级:左子树 -> 根结点 -> 右子树
 | 
				
			||||||
 | 
					        in_order(root=root.left)
 | 
				
			||||||
 | 
					        res.append(root.val)
 | 
				
			||||||
 | 
					        in_order(root=root.right)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    """ 后序遍历 """
 | 
				
			||||||
 | 
					    def post_order(root: typing.Optional[TreeNode]):
 | 
				
			||||||
 | 
					        if root is None:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        # 访问优先级:左子树 -> 右子树 -> 根结点
 | 
				
			||||||
 | 
					        post_order(root=root.left)
 | 
				
			||||||
 | 
					        post_order(root=root.right)
 | 
				
			||||||
 | 
					        res.append(root.val)
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "Go"
 | 
					=== "Go"
 | 
				
			||||||
@ -402,7 +441,6 @@ comments: true
 | 
				
			|||||||
        postOrder(root.right);
 | 
					        postOrder(root.right);
 | 
				
			||||||
        list.Add(root.val);
 | 
					        list.Add(root.val);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
!!! note
 | 
					!!! note
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user