golang二叉树翻转(递归与非递归实现)

二叉树翻转
在这里插入图片描述

树的初始化可以粗暴一些。
如果要看一棵比较简单的树的结构,可以用json包序列化打印出来看

非递归实现

package main

import (
	"encoding/json"
	"fmt"
	"github.com/eapache/queue"
)

type Node struct {
	Key   int
	Left  *Node
	Right *Node
}

func (n *Node) Mirror() {
	q := queue.New()
	q.Add(n)

	for {
		if q.Length() <= 0 {
			break
		}
		node := q.Remove().(*Node)
		if node == nil {
			break
		}

		tmp := node.Left
		node.Left = node.Right
		node.Right = tmp

		if node.Left != nil {
			q.Add(node.Left)
		}

		if node.Right != nil {
			q.Add(node.Right)
		}

	}
}

func main() {
	root := &Node{Key: 4}

	root.Left = &Node{
		Key: 2,
		Left: &Node{
			Key:   1,
			Left:  nil,
			Right: nil,
		},
		Right: &Node{
			Key:   3,
			Left:  nil,
			Right: nil,
		},
	}

	root.Right = &Node{
		Key: 7,
		Left: &Node{
			Key:   6,
			Left:  nil,
			Right: nil,
		},
		Right: &Node{
			Key:   9,
			Left:  nil,
			Right: nil,
		},
	}
	root.Mirror()
	out, _ := json.MarshalIndent(root, "", "    ")
	fmt.Println(string(out))


}


输出

{
    "Key": 4,
    "Left": {
        "Key": 7,
        "Left": {
            "Key": 9,
            "Left": null,
            "Right": null
        },
        "Right": {
            "Key": 6,
            "Left": null,
            "Right": null
        }
    },
    "Right": {
        "Key": 2,
        "Left": {
            "Key": 3,
            "Left": null,
            "Right": null
        },
        "Right": {
            "Key": 1,
            "Left": null,
            "Right": null
        }
    }
}

递归实现

package main

import (
	"encoding/json"
	"fmt"
)

type Node struct {
	Key   int
	Left  *Node
	Right *Node
}

func (n *Node) Mirror2() {
	tmp := n.Left
	n.Left = n.Right
	n.Right = tmp

	if n.Left != nil {
		n.Left.Mirror2()
	}
	if n.Right != nil {
		n.Right.Mirror2()
	}
}

func main() {
	root := &Node{Key: 4}

	root.Left = &Node{
		Key: 2,
		Left: &Node{
			Key:   1,
			Left:  nil,
			Right: nil,
		},
		Right: &Node{
			Key:   3,
			Left:  nil,
			Right: nil,
		},
	}

	root.Right = &Node{
		Key: 7,
		Left: &Node{
			Key:   6,
			Left:  nil,
			Right: nil,
		},
		Right: &Node{
			Key:   9,
			Left:  nil,
			Right: nil,
		},
	}
	root.Mirror2()
	out, _ := json.MarshalIndent(root, "", "    ")
	fmt.Println(string(out))
}

输出

{
    "Key": 4,
    "Left": {
        "Key": 7,
        "Left": {
            "Key": 9,
            "Left": null,
            "Right": null
        },
        "Right": {
            "Key": 6,
            "Left": null,
            "Right": null
        }
    },
    "Right": {
        "Key": 2,
        "Left": {
            "Key": 3,
            "Left": null,
            "Right": null
        },
        "Right": {
            "Key": 1,
            "Left": null,
            "Right": null
        }
    }
}

以下是使用Golang GORM进行MySQL递归查询单表的示例代码: ```go package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" ) type Category struct { ID int Name string ParentID int Children []Category `gorm:"foreignkey:ParentID"` } func main() { db, err := gorm.Open("mysql", "user:password@tcp(127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local") if err != nil { panic(err) } defer db.Close() var categories []Category db.Where("parent_id = ?", 0).Preload("Children").Find(&categories) for _, category := range categories { fmt.Println(category.Name) for _, child := range category.Children { fmt.Println(" ", child.Name) } } } ``` 在这个示例中,我们定义了一个Category结构体,其中包含ID、Name、ParentID和Children字段。Children字段是一个Category类型的切片,用于存储子类别。在结构体中,我们使用了GORM的foreignkey标记来指定ParentID字段是外键,Children字段是通过ParentID字段与Category表关联的。 在main函数中,我们首先使用GORM的Open函数打开MySQL数据库连接。然后,我们定义了一个categories切片,用于存储查询结果。我们使用GORM的Where函数指定ParentID为0,即查询所有顶级类别。然后,我们使用GORM的Preload函数预加载Children字段,以便在查询结果中包含子类别。最后,我们使用GORM的Find函数执行查询,并将结果存储在categories切片中。 最后,我们遍历categories切片,并打印每个类别及其子类别的名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值