|
|
/**
|
|
|
* File: TreeNode.swift
|
|
|
* Created Time: 2023-01-02
|
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
|
*/
|
|
|
|
|
|
/* 二叉树节点类 */
|
|
|
public class TreeNode {
|
|
|
public var val: Int // 节点值
|
|
|
public var height: Int // 节点高度
|
|
|
public var left: TreeNode? // 左子节点引用
|
|
|
public var right: TreeNode? // 右子节点引用
|
|
|
|
|
|
/* 构造方法 */
|
|
|
public init(x: Int) {
|
|
|
val = x
|
|
|
height = 0
|
|
|
}
|
|
|
|
|
|
// 序列化编码规则请参考:
|
|
|
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
|
|
// 二叉树的数组表示:
|
|
|
// [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
|
|
// 二叉树的链表表示:
|
|
|
// /——— 15
|
|
|
// /——— 7
|
|
|
// /——— 3
|
|
|
// | \——— 6
|
|
|
// | \——— 12
|
|
|
// ——— 1
|
|
|
// \——— 2
|
|
|
// | /——— 9
|
|
|
// \——— 4
|
|
|
// \——— 8
|
|
|
|
|
|
/* 将列表反序列化为二叉树:递归 */
|
|
|
private static func listToTreeDFS(arr: [Int?], i: Int) -> TreeNode? {
|
|
|
if i < 0 || i >= arr.count || arr[i] == nil {
|
|
|
return nil
|
|
|
}
|
|
|
let root = TreeNode(x: arr[i]!)
|
|
|
root.left = listToTreeDFS(arr: arr, i: 2 * i + 1)
|
|
|
root.right = listToTreeDFS(arr: arr, i: 2 * i + 2)
|
|
|
return root
|
|
|
}
|
|
|
|
|
|
/* 将列表反序列化为二叉树 */
|
|
|
public static func listToTree(arr: [Int?]) -> TreeNode? {
|
|
|
listToTreeDFS(arr: arr, i: 0)
|
|
|
}
|
|
|
|
|
|
/* 将二叉树序列化为列表:递归 */
|
|
|
private static func treeToListDFS(root: TreeNode?, i: Int, res: inout [Int?]) {
|
|
|
if root == nil {
|
|
|
return
|
|
|
}
|
|
|
while i >= res.count {
|
|
|
res.append(nil)
|
|
|
}
|
|
|
res[i] = root?.val
|
|
|
treeToListDFS(root: root?.left, i: 2 * i + 1, res: &res)
|
|
|
treeToListDFS(root: root?.right, i: 2 * i + 2, res: &res)
|
|
|
}
|
|
|
|
|
|
/* 将二叉树序列化为列表 */
|
|
|
public static func treeToList(root: TreeNode?) -> [Int?] {
|
|
|
var res: [Int?] = []
|
|
|
treeToListDFS(root: root, i: 0, res: &res)
|
|
|
return res
|
|
|
}
|
|
|
}
|