2023-09-02 23:08:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: build_tree.swift
|
|
|
|
|
* Created Time: 2023-09-02
|
|
|
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import utils
|
|
|
|
|
|
|
|
|
|
/* 构建二叉树:分治 */
|
2023-09-15 02:08:09 +08:00
|
|
|
|
func dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {
|
2023-09-02 23:08:37 +08:00
|
|
|
|
// 子树区间为空时终止
|
|
|
|
|
if r - l < 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
// 初始化根节点
|
|
|
|
|
let root = TreeNode(x: preorder[i])
|
|
|
|
|
// 查询 m ,从而划分左右子树
|
2023-09-15 02:08:09 +08:00
|
|
|
|
let m = inorderMap[preorder[i]]!
|
2023-09-02 23:08:37 +08:00
|
|
|
|
// 子问题:构建左子树
|
2023-09-15 02:08:09 +08:00
|
|
|
|
root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)
|
2023-09-02 23:08:37 +08:00
|
|
|
|
// 子问题:构建右子树
|
2023-09-15 02:08:09 +08:00
|
|
|
|
root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)
|
2023-09-02 23:08:37 +08:00
|
|
|
|
// 返回根节点
|
|
|
|
|
return root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 构建二叉树 */
|
|
|
|
|
func buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {
|
|
|
|
|
// 初始化哈希表,存储 inorder 元素到索引的映射
|
2023-09-15 02:08:09 +08:00
|
|
|
|
let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
2024-03-20 21:15:39 +08:00
|
|
|
|
return dfs(preorder: preorder, inorderMap: inorderMap, i: inorder.startIndex, l: inorder.startIndex, r: inorder.endIndex - 1)
|
2023-09-02 23:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@main
|
|
|
|
|
enum BuildTree {
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
static func main() {
|
|
|
|
|
let preorder = [3, 9, 2, 1, 7]
|
|
|
|
|
let inorder = [9, 3, 1, 2, 7]
|
|
|
|
|
print("前序遍历 = \(preorder)")
|
|
|
|
|
print("中序遍历 = \(inorder)")
|
|
|
|
|
|
|
|
|
|
let root = buildTree(preorder: preorder, inorder: inorder)
|
|
|
|
|
print("构建的二叉树为:")
|
|
|
|
|
PrintUtil.printTree(root: root)
|
|
|
|
|
}
|
|
|
|
|
}
|