mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 10:53:35 +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
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
// File: tree_node.go
|
|
// Created Time: 2022-11-25
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package pkg
|
|
|
|
// TreeNode binary tree node
|
|
type TreeNode struct {
|
|
Val any // Node value
|
|
Height int // Node height
|
|
Left *TreeNode // Reference to left child node
|
|
Right *TreeNode // Reference to right child node
|
|
}
|
|
|
|
// NewTreeNode binary tree node constructor
|
|
func NewTreeNode(v any) *TreeNode {
|
|
return &TreeNode{
|
|
Val: v,
|
|
Height: 0,
|
|
Left: nil,
|
|
Right: nil,
|
|
}
|
|
}
|
|
|
|
// 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
|
|
|
|
// SliceToTreeDFS deserialize list to binary tree: recursion
|
|
func SliceToTreeDFS(arr []any, i int) *TreeNode {
|
|
if i < 0 || i >= len(arr) || arr[i] == nil {
|
|
return nil
|
|
}
|
|
root := NewTreeNode(arr[i])
|
|
root.Left = SliceToTreeDFS(arr, 2*i+1)
|
|
root.Right = SliceToTreeDFS(arr, 2*i+2)
|
|
return root
|
|
}
|
|
|
|
// SliceToTree deserialize slice to binary tree
|
|
func SliceToTree(arr []any) *TreeNode {
|
|
return SliceToTreeDFS(arr, 0)
|
|
}
|
|
|
|
// TreeToSliceDFS serialize binary tree to slice: recursion
|
|
func TreeToSliceDFS(root *TreeNode, i int, res *[]any) {
|
|
if root == nil {
|
|
return
|
|
}
|
|
for i >= len(*res) {
|
|
*res = append(*res, nil)
|
|
}
|
|
(*res)[i] = root.Val
|
|
TreeToSliceDFS(root.Left, 2*i+1, res)
|
|
TreeToSliceDFS(root.Right, 2*i+2, res)
|
|
}
|
|
|
|
// TreeToSlice serialize binary tree to slice
|
|
func TreeToSlice(root *TreeNode) []any {
|
|
var res []any
|
|
TreeToSliceDFS(root, 0, &res)
|
|
return res
|
|
}
|