Golang实现二叉树路径遍历


本文摘自网络,作者,侵删。

package main

import "fmt"

type Node struct {
    left  *Node
    right *Node
    value string
}

var res [][]string

func main() {
    var path []string
    e := Node{nil, nil, "5"}
    d := Node{nil, nil, "4"}
    c := Node{left: nil, right: &e, value: "3"}
    b := Node{nil, nil, "6"}
    a := Node{left: &c, right: &d, value: "2"}
    head := Node{left: &a, right: &b, value: "1"}
    BinaryTreePath(&head, path)
    fmt.Println(res)
}

func BinaryTreePath(root *Node, path []string) {
    path = append(path, root.value)
    if root.left == nil && root.right == nil {
        res = append(res, path)
    }
    if root.left != nil {
        BinaryTreePath(root.left, path)
    }
    if root.right != nil {
        BinaryTreePath(root.right, path)
    }
}

本文来自:简书

感谢作者:看活一分钟

查看原文:Golang实现二叉树路径遍历

相关阅读 >>

Golang使用for循环的一个小技巧

Golang底层是c语言吗?

一周 Go world 新鲜事

Golang如何删除文件?

手撸Golang 基本数据结构与算法 栈

聊聊dubbo-Go-proxy的replacepathfilter

Go - 统一定义 api 错误码

如何使用Go优雅地撰写单元测试

多协程通道的应用---三协程通过通道实现abc按顺序输出100次

聊聊Golang的zap的sink

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...