mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-07 04:43:50 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
72 lines
2.2 KiB
Swift
72 lines
2.2 KiB
Swift
/**
|
|
* File: TreeNode.swift
|
|
* Created Time: 2023-01-02
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
*/
|
|
|
|
/* Binary tree node class */
|
|
public class TreeNode {
|
|
public var val: Int // Node value
|
|
public var height: Int // Node height
|
|
public var left: TreeNode? // Reference to left child node
|
|
public var right: TreeNode? // Reference to right child node
|
|
|
|
/* Constructor */
|
|
public init(x: Int) {
|
|
val = x
|
|
height = 0
|
|
}
|
|
|
|
// For the serialization encoding rules, please refer to:
|
|
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
|
// Array representation of binary tree:
|
|
// [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
|
// Linked list representation of binary tree:
|
|
// /——— 15
|
|
// /——— 7
|
|
// /——— 3
|
|
// | \——— 6
|
|
// | \——— 12
|
|
// ——— 1
|
|
// \——— 2
|
|
// | /——— 9
|
|
// \——— 4
|
|
// \——— 8
|
|
|
|
/* Deserialize a list into a binary tree: recursion */
|
|
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
|
|
}
|
|
|
|
/* Deserialize a list into a binary tree */
|
|
public static func listToTree(arr: [Int?]) -> TreeNode? {
|
|
listToTreeDFS(arr: arr, i: 0)
|
|
}
|
|
|
|
/* Serialize a binary tree into a list: recursion */
|
|
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)
|
|
}
|
|
|
|
/* Serialize a binary tree into a list */
|
|
public static func treeToList(root: TreeNode?) -> [Int?] {
|
|
var res: [Int?] = []
|
|
treeToListDFS(root: root, i: 0, res: &res)
|
|
return res
|
|
}
|
|
}
|