mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 19:03:42 +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
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
"""
|
|
File: tree_node.py
|
|
Created Time: 2021-12-11
|
|
Author: krahets (krahets@163.com)
|
|
"""
|
|
|
|
from collections import deque
|
|
|
|
|
|
class TreeNode:
|
|
"""Binary tree node class"""
|
|
|
|
def __init__(self, val: int = 0):
|
|
self.val: int = val # Node value
|
|
self.height: int = 0 # Node height
|
|
self.left: TreeNode | None = None # Reference to left child node
|
|
self.right: TreeNode | None = None # Reference to right child node
|
|
|
|
# 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, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
|
# Linked list representation of binary tree:
|
|
# /——— 15
|
|
# /——— 7
|
|
# /——— 3
|
|
# | \——— 6
|
|
# | \——— 12
|
|
# ——— 1
|
|
# \——— 2
|
|
# | /——— 9
|
|
# \——— 4
|
|
# \——— 8
|
|
|
|
|
|
def list_to_tree_dfs(arr: list[int], i: int) -> TreeNode | None:
|
|
"""Deserialize a list into a binary tree: recursion"""
|
|
# If the index exceeds the array length, or the corresponding element is None, return None
|
|
if i < 0 or i >= len(arr) or arr[i] is None:
|
|
return None
|
|
# Build the current node
|
|
root = TreeNode(arr[i])
|
|
# Recursively build the left and right subtrees
|
|
root.left = list_to_tree_dfs(arr, 2 * i + 1)
|
|
root.right = list_to_tree_dfs(arr, 2 * i + 2)
|
|
return root
|
|
|
|
|
|
def list_to_tree(arr: list[int]) -> TreeNode | None:
|
|
"""Deserialize a list into a binary tree"""
|
|
return list_to_tree_dfs(arr, 0)
|
|
|
|
|
|
def tree_to_list_dfs(root: TreeNode, i: int, res: list[int]) -> list[int]:
|
|
"""Serialize a binary tree into a list: recursion"""
|
|
if root is None:
|
|
return
|
|
if i >= len(res):
|
|
res += [None] * (i - len(res) + 1)
|
|
res[i] = root.val
|
|
tree_to_list_dfs(root.left, 2 * i + 1, res)
|
|
tree_to_list_dfs(root.right, 2 * i + 2, res)
|
|
|
|
|
|
def tree_to_list(root: TreeNode | None) -> list[int]:
|
|
"""Serialize a binary tree into a list"""
|
|
res = []
|
|
tree_to_list_dfs(root, 0, res)
|
|
return res
|