2023-01-06 03:39:19 +08:00
|
|
|
|
/**
|
2023-01-02 21:48:32 +08:00
|
|
|
|
* File: TreeNode.swift
|
|
|
|
|
* Created Time: 2023-01-02
|
|
|
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-24 12:46:48 +08:00
|
|
|
|
/* 二叉树节点类 */
|
2023-01-02 21:48:32 +08:00
|
|
|
|
public class TreeNode {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
public var val: Int // 节点值
|
|
|
|
|
public var height: Int // 节点高度
|
|
|
|
|
public var left: TreeNode? // 左子节点引用
|
|
|
|
|
public var right: TreeNode? // 右子节点引用
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
2023-07-24 12:46:48 +08:00
|
|
|
|
/* 构造方法 */
|
2023-01-02 21:48:32 +08:00
|
|
|
|
public init(x: Int) {
|
|
|
|
|
val = x
|
|
|
|
|
height = 0
|
|
|
|
|
}
|
2023-01-19 00:12:54 +08:00
|
|
|
|
|
2023-07-24 12:46:48 +08:00
|
|
|
|
// 序列化编码规则请参考:
|
|
|
|
|
// 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 {
|
2023-01-19 00:12:54 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-07-24 12:46:48 +08:00
|
|
|
|
let root = TreeNode(x: arr[i]!)
|
|
|
|
|
root.left = listToTreeDFS(arr: arr, i: 2 * i + 1)
|
|
|
|
|
root.right = listToTreeDFS(arr: arr, i: 2 * i + 2)
|
2023-01-19 00:12:54 +08:00
|
|
|
|
return root
|
|
|
|
|
}
|
2023-07-24 12:46:48 +08:00
|
|
|
|
|
|
|
|
|
/* 将列表反序列化为二叉树 */
|
|
|
|
|
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
|
|
|
|
|
}
|
2023-01-02 21:48:32 +08:00
|
|
|
|
}
|