Ai
4 Star 12 Fork 8

ShirDon-廖显东/零基础Go语言算法实战源码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
binaryTree.go 1.10 KB
一键复制 编辑 原始数据 按行查看 历史
ShirDon-廖显东 提交于 2024-04-22 14:56 +08:00 . first commit
// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
)
// 定义节点
type TreeNode struct {
//数据项
Data int
//左子树的地址
Left *TreeNode
//右子树的地址
Right *TreeNode
}
// 插入节点
func Insert(root *TreeNode) []int {
if root == nil {
return nil
}
left := Insert(root.Left)
right := Insert(root.Right)
output := make([]int, 0)
output = append(output, left...)
output = append(output, root.Data)
output = append(output, right...)
return output
}
func main() {
root := TreeNode{Data: 1}
root.Left = &TreeNode{Data: 2}
root.Left.Left = &TreeNode{Data: 4}
root.Right = &TreeNode{Data: 3}
root.Right.Left = &TreeNode{Data: 5}
root.Right.Right = &TreeNode{Data: 6}
output := Insert(&root)
fmt.Println(output)
}
//$ go run binaryTree.go
//[4 2 1 5 3 6]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shirdonl/goAlgorithms.git
git@gitee.com:shirdonl/goAlgorithms.git
shirdonl
goAlgorithms
零基础Go语言算法实战源码
3e77a12194dd

搜索帮助