package main
import "fmt"
type Node struct {
Data int
Left *Node
Right *Node
}
func preOrder(n *Node) {
if n == nil {
return
}
fmt.Println(n.Data)
preOrder(n.Left)
preOrder(n.Right)
}
func midOrder(n *Node) {
if n == nil {
return
}
midOrder(n.Left)
fmt.Println(n.Data)
midOrder(n.Right)
}
func aftOrder(n *Node) {
if n == nil {
return
}
aftOrder(n.Left)
aftOrder(n.Right)
fmt.Println(n.Data)
}
func main() {
a := &Node{Data: 1}
b := &Node{Data: 2}
c := &Node{Data: 3}
d := &Node{Data: 4}
e := &Node{Data: 5}
f := &Node{Data: 6}
g := &Node{Data: 7}
a.Left = b
a.Right = e
b.Left = c
b.Right = d
e.Left = f
e.Right = g
preOrder(a) // 前序遍历结果:1,2,3,4,5,6,7
midOrder(a) // 中序遍历结果:3,2,4,1,6,5,7
aftOrder(a) // 后序遍历结果:3,4,2,6,7,5,1
}