feat: Add the chapter of divide and conquer (#609)
* Add the chapter of divide and conquer. Add the section of divide and conquer algorithm. Add the section of build tree problem. * Update build_tree.pypull/612/head
@ -0,0 +1,54 @@
|
||||
"""
|
||||
File: build_tree.py
|
||||
Created Time: 2023-07-15
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
|
||||
|
||||
def dfs(
|
||||
preorder: list[int],
|
||||
inorder: list[int],
|
||||
hmap: dict[int, int],
|
||||
i: int,
|
||||
l: int,
|
||||
r: int,
|
||||
) -> TreeNode | None:
|
||||
"""构建二叉树 DFS"""
|
||||
# 子树区间为空时终止
|
||||
if r - l < 0:
|
||||
return None
|
||||
# 初始化根节点
|
||||
root = TreeNode(preorder[i])
|
||||
# 查询 m ,从而划分左右子树
|
||||
m = hmap[preorder[i]]
|
||||
# 递归构建左子树
|
||||
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1)
|
||||
# 递归构建右子树
|
||||
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r)
|
||||
# 返回根节点
|
||||
return root
|
||||
|
||||
|
||||
def build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:
|
||||
"""构建二叉树"""
|
||||
# 初始化哈希表,存储 inorder 元素到索引的映射
|
||||
hmap = {val: i for i, val in enumerate(inorder)}
|
||||
root = dfs(preorder, inorder, hmap, 0, 0, len(inorder) - 1)
|
||||
return root
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
preorder = [3, 9, 2, 1, 7]
|
||||
inorder = [9, 3, 1, 2, 7]
|
||||
print(f"前序遍历 = {preorder}")
|
||||
print(f"中序遍历 = {inorder}")
|
||||
|
||||
root = build_tree(preorder, inorder)
|
||||
print("构建的二叉树为:")
|
||||
print_tree(root)
|
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 52 KiB |
After Width: | Height: | Size: 82 KiB |
After Width: | Height: | Size: 41 KiB |
After Width: | Height: | Size: 83 KiB |
After Width: | Height: | Size: 41 KiB |
After Width: | Height: | Size: 45 KiB |
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 46 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 57 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 88 KiB |
After Width: | Height: | Size: 56 KiB |
@ -0,0 +1,2 @@
|
||||
# 分治
|
||||
|