2023-07-16 04:24:04 +08:00
|
|
|
"""
|
|
|
|
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],
|
2023-09-15 02:08:09 +08:00
|
|
|
inorder_map: dict[int, int],
|
2023-07-16 04:24:04 +08:00
|
|
|
i: int,
|
|
|
|
l: int,
|
|
|
|
r: int,
|
|
|
|
) -> TreeNode | None:
|
2023-07-17 02:18:53 +08:00
|
|
|
"""构建二叉树:分治"""
|
2023-07-16 04:24:04 +08:00
|
|
|
# 子树区间为空时终止
|
|
|
|
if r - l < 0:
|
|
|
|
return None
|
|
|
|
# 初始化根节点
|
|
|
|
root = TreeNode(preorder[i])
|
|
|
|
# 查询 m ,从而划分左右子树
|
2023-09-15 02:08:09 +08:00
|
|
|
m = inorder_map[preorder[i]]
|
2023-07-17 02:18:53 +08:00
|
|
|
# 子问题:构建左子树
|
2023-09-15 02:08:09 +08:00
|
|
|
root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)
|
2023-07-17 02:18:53 +08:00
|
|
|
# 子问题:构建右子树
|
2023-09-15 02:08:09 +08:00
|
|
|
root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)
|
2023-07-16 04:24:04 +08:00
|
|
|
# 返回根节点
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
|
|
def build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:
|
|
|
|
"""构建二叉树"""
|
|
|
|
# 初始化哈希表,存储 inorder 元素到索引的映射
|
2023-09-15 02:08:09 +08:00
|
|
|
inorder_map = {val: i for i, val in enumerate(inorder)}
|
|
|
|
root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)
|
2023-07-16 04:24:04 +08:00
|
|
|
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)
|