golang数据结构之树的三种遍历方式

前端之家收集整理的这篇文章主要介绍了golang数据结构之树的三种遍历方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

tree.go

package tree

import (
    "fmt"
)

type TreeNode struct {
    ID    int
    Val   
    Left  *TreeNode
    Right *TreeNode
}

func PreOrder(root *TreeNode) {
    if root != nil {
        fmt.Printf(%d ,root.Val)
        PreOrder(root.Left)
        PreOrder(root.Right)
    }
}

func InOrder(root * nil {
        InOrder(root.Left)
        fmt.Printf( nil {
        PostOrder(root.Left)
        PostOrder(root.Right)
        fmt.Printf(
package main

import (
    "
    go_code/data_structure/tree
)

func main() {

    node7 := &tree.TreeNode{
        ID:    7= &654321先序遍历)
    tree.PreOrder(node1)
    fmt.Println()
    fmt.Println(中序遍历)
    tree.InOrder(node1)
    fmt.Println()
    fmt.Println(后序遍历)
    tree.PostOrder(node1)
}

运行结果:

猜你在找的Go相关文章