二叉树深度优先遍历

package program

import "testing"

type TreeNode struct {
    Value int
    Left  *TreeNode
    Right *TreeNode
}

func TestTreeDeepSearch(t *testing.T) {
    //构造一个随机value的树
    root := &TreeNode{Value: 1}
    root.Left = &TreeNode{Value: 2, Left: &TreeNode{Value: 4}, Right: &TreeNode{Value: 5}}
    root.Right = &TreeNode{Value: 3, Left: &TreeNode{Value: 6}, Right: &TreeNode{Value: 7}}

    printTree(root)
}

func printTree(root *TreeNode) {
    if root == nil {
        return
    }
    println(root.Value)
    printTree(root.Left)
    printTree(root.Right)
}

results matching ""

    No results matching ""