当前位置: 首页 > news >正文

go 判断两棵树内容是否一致

goroutine

http://127.0.0.1:3999/concurrency/8

question

使用 go 判断 两个 树 存放的 序列 是否 相同, 如果 相同 他们 被称为 equivalent_tree

tree struct


type Tree struct {Left  *TreeValue intRight *Tree
}

由于 递归的 写法 比较简单, 此处 使用循环的 形式 来实现.
因为 循环的 写法 不存在 调用栈的 瓶颈, 所以 在实际工程中, 能够 避免 递归 还是 避免递归吧.
judge tree

package mainimport "golang.org/x/tour/tree"
import "fmt"// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {_ts := make(chan *tree.Tree, 100)_ts <- t
OuterLoop:for {_f1 := func(_tmp *tree.Tree) {fmt.Println(_tmp.Value)ch <- _tmp.Valuefor _, v := range []*tree.Tree{_tmp.Left, _tmp.Right} {if v != nil {_ts <- v}}}select {case _tmp := <-_ts:_f1(_tmp)default:fmt.Println("break")break OuterLoop}}
}// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {return true
}func main() {_t1 := tree.New(1)_c1 := make(chan int, 100)Walk(_t1, _c1)_t2 := tree.New(1)_c2 := make(chan int, 100)Walk(_t2, _c2)fmt.Println(_c)
}
http://www.lryc.cn/news/238273.html

相关文章:

  • 从Hugging Face上手动下载并加载预训练模型
  • Vue 前置 后置 路由守卫 独享 路由权限控制 自定义属性
  • upload-labs关卡11(双写后缀名绕过)通关思路
  • go语言学习之旅之Go语言基础语法二
  • 七天.NET 8操作SQLite入门到实战 - SQLite 简介
  • 问题 R: 胜利大逃亡(HUST)
  • 项目讲解:让你在IT行业面试中以开发、实施、产品更近一步
  • Windows 安装 Docker
  • AI绘画使用Stable Diffusion(SDXL)绘制三星堆风格的图片
  • Window下如何对Redis进行开启与关闭
  • C++ Qt 学习(十):Qt 其他技巧
  • 跳台阶游戏(Python排列组合函数itertools.combinations的应用)
  • 【Python百宝箱】Python测试工具大揭秘:从单元测试到Web自动化
  • 〖大前端 - 基础入门三大核心之JS篇㊵〗- DOM事件监听及onxxx的使用
  • 解锁潜力:创建支持Actions接口调用的高级GPTs
  • 【发明专利】天洑软件再度收获六项国家发明专利授权
  • Netty源码学习4——服务端是处理新连接的netty的reactor模式
  • 8、信息打点——系统篇端口扫描CDN服务负载均衡WAF
  • Ant Design for Figma设计系统组件库 支持变量 非社区版
  • k8s的高可用集群搭建,详细过程实战版
  • 【20年扬大真题】编写对数组求逆的递归算法
  • 日志门面slf4j与常用的日志框架Log4j,Logback和Log4j2
  • 使用ssh在本地环境(Windows)连接虚拟机以及其中的docker容器
  • 没收到Win11 23H2正式版的推送怎么升级到23H2
  • SpringBoot整合Redis使用基于注解的缓存
  • STM32:时钟树原理概要
  • Python量化--诺贝尔奖获得者布莱克-斯科尔斯期权定价公式在日间交易中的应用
  • Redis 5 种基本数据类型详解
  • LeetCode8-字符串转换整数(atoi)
  • 算法分析与设计课后练习22