二叉树定义
type Student struct {
Name string
left* Student
right* Student
}
如果每个节点有两个指针分别用来指向左子树和右子树,我们把这样的结构叫做二叉树
example01
package main
import "fmt"
type Student struct {
Name string
Age int
Score float32
left *Student
right *Student
}
func trans(root *Student) {
if root == nil {
return
}
fmt.Println(root)
trans(root.left)
trans(root.right)
}
func main() {
var root *Student = new(Student)
root.Name = "stu01"
root.Age = 18
root.Score = 100
var left1 *Student = new(Student)
left1.Name = "stu02"
left1.Age = 18
left1.Score = 100
root.left = left1
var right1 *Student = new(Student)
right1.Name = "stu04"
right1.Age = 18
right1.Score = 100
root.right = right1
var left2 *Student = new(Student)
left2.Name = "stu03"
left2.Age = 18
left2.Score = 100
left1.left = left2
trans(root)
}
打印结果:
&{stu01 18 100 0xc000068360 0xc000068390}
&{stu02 18 100 0xc0000683c0 }
&{stu03 18 100 }
&{stu04 18 100 }
二叉树结构与遍历
400

被折叠的 条评论
为什么被折叠?



