mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 18:37:48 +08:00 
			
		
		
		
	 034ee65e9a
			
		
	
	034ee65e9a
	
	
	
		
			
			* Fix the comment in array_deque.go * Fix the comment in bucket_sort.c * Translate the Java code comments to Chinese * Bug fixes * 二分查找 -> 二分搜尋 * Harmonize comments in `utils` between multiple programming languages
		
			
				
	
	
		
			79 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // File: tree_node.go
 | |
| // Created Time: 2022-11-25
 | |
| // Author: Reanon (793584285@qq.com)
 | |
| 
 | |
| package pkg
 | |
| 
 | |
| // TreeNode 二叉树节点
 | |
| type TreeNode struct {
 | |
| 	Val    any       // 节点值
 | |
| 	Height int       // 节点高度
 | |
| 	Left   *TreeNode // 左子节点引用
 | |
| 	Right  *TreeNode // 右子节点引用
 | |
| }
 | |
| 
 | |
| // NewTreeNode 二叉树节点构造函数
 | |
| func NewTreeNode(v any) *TreeNode {
 | |
| 	return &TreeNode{
 | |
| 		Val:    v,
 | |
| 		Height: 0,
 | |
| 		Left:   nil,
 | |
| 		Right:  nil,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // 序列化编码规则请参考:
 | |
| // 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
 | |
| 
 | |
| // SliceToTreeDFS 将列表反序列化为二叉树:递归
 | |
| 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 将切片反序列化为二叉树
 | |
| func SliceToTree(arr []any) *TreeNode {
 | |
| 	return SliceToTreeDFS(arr, 0)
 | |
| }
 | |
| 
 | |
| // TreeToSliceDFS 将二叉树序列化为切片:递归
 | |
| 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 将二叉树序列化为切片
 | |
| func TreeToSlice(root *TreeNode) []any {
 | |
| 	var res []any
 | |
| 	TreeToSliceDFS(root, 0, &res)
 | |
| 	return res
 | |
| }
 |