A Tour of Go - Exercise: Equivalent Binary Trees

使用Golang语言walk框架开发一个简单的windowsGUI 一个用walk框架写的windowsGUI示例(golang) 功能点包括:输入框,密码行,登录,系统托盘,页面切换,窗口最大化禁用,窗口尺寸禁止修改等 阅读详情

Exercise: Equivalent Binary Trees

There can be many different binary trees with the same sequence of values stored at the leaves. For example, here are two binary trees storing the sequence 1, 1, 2, 3, 5, 8, 13.binary trees

A function to check whether two binary trees store the same sequence is quite complex in most languages. We'll use Go's concurrency and channels to write a simple solution.

This example uses the tree package, which defines the type:

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

1. Implement the Walk function.

2. Test the Walk function.

The function tree.New(k) constructs a randomly-structured binary tree holding the values k, 2k, 3k, ..., 10k.

Create a new channel ch and kick off the walker:

go Walk(tree.New(1), ch)

Then read and print 10 values from the channel. It should be the numbers 1, 2, 3, ..., 10.

3. Implement the Same function using Walk to determine whether t1 and t2store the same values.

4. Test the Same function.

Same(tree.New(1), tree.New(1)) should return true, and Same(tree.New(1), tree.New(2)) should return false.



package main

import "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) {
	if t.Left != nil {
		Walk(t.Left, ch)
	}
	ch <- t.Value 
	if t.Right != nil {
		Walk(t.Right, ch)
	}
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
	ch1 := make(chan int)
	ch2 := make(chan int)
	
	go Walk(t1, ch1)
	go Walk(t2, ch2)

	result := true
	for i := 0; i < 10; i ++ {
		v1 := <- ch1
		v2 := <- ch2
		result = (v1 == v2)
	}
	
	return result
	
}

func main() {
//	ch := make(chan int)
//	go Walk(tree.New(1), ch)
//	for i := 0; i < 10; i ++ {
//		v := <- ch
//		fmt.Println(v)
//	}
	
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
	
}


Go窗体练习(Walk包) Go窗体学习(Walk包)前言准备代码结果 前言 由于老师让我们做一个窗体程序,正巧我在学习go,于是搜索了一下go是否能做窗体,结果就发现了walk包,题目为字符串解析(大概就是输出一下前后缀等等) 准备 安装github上的 lxn/walk 包,以及 rsrc 包 go get github.com/lxn/walk go get github.com/akavel/rsrc 接着创建一个manifest程序,我这里直接复制的官方文档,命名为test.manifest,接着运行 <?xml ve 阅读详情

相关推荐

golang官方指导练习(go tour exercise)

golang官方练习的pass代码,使用了中文版的tour

掠雪墨影的随记 8054

Exercise: Equivalent Binary Trees (二叉树对比)

A Tour of Go Exercise: Equivalent Binary Trees https://tour.golang.org/concurrency/7 检查两棵二叉树是否包含相同的数值,这题难度稍大,有几个难点。 答案如下: 其中,函数 walk 用递归方法遍历二叉树 在 walk 的外面再包裹一层 Walk 是为了 close(ch) 而clos...

weixin_30586257的博客 270

Go语言练习:等价二叉查找树

题目要求 实现 Walk 函数。 测试 Walk 函数。 函数 tree.New(k) 用于构造一个随机结构的已排序二叉查找树,它保存了值 k, 2k, 3k, …, 10k。 创建一个新的信道 ch 并且对其进行步进: go Walk(tree.New(1), ch) 然后从信道中读取并打印 10 个值。应当是数字 1, 2, 3, …, 10。 用 Walk 实现 Same 函...

liuYinXin的博客 2539

go语言Exercise: Equivalent Binary Trees

1. Implement the Walk function. 2. Test the Walk function. The function tree.New(k) constructs a randomly-structured binary tree holding the values k, 2k, 3k, ..., 10k. Create a new channel ch an

一剪梅 2112

【电力系统】高压直流输电VSC-HVDC,四个变流站联合输电 / 交直流混合并网系统

本文研究了一种基于电压源型换流器(VSC)的高压直流输电(HVDC)系统,该系统通过四个变流站实现联合输电并接入交直流混合电网。文章介绍了系统的结构、工作原理以及控制策略,分析了交直流混合并网的特性,并通过仿真验证了系统的性能。结果表明,该系统具有良好的稳定性和灵活性,能够有效实现多点供电和功率协调。

2401_84610415的博客 2239

【软考高项】五十一、常见必会的几种进度网络图(单代号、双代号、七格图、横道图)

软考高项计算题进度网络图画图介绍

有莘不破的博客 3万+

Go Exercise: Equivalent Binary Trees

packagemain import( "fmt" "tour/tree" ) //Walk walks the tree t sending all values //from the tree to the channel ch. funcWalk(t *tree.Tree, ch chan int) { ift.

陌上 3530

B站充电视频下载器(需配合会员Cookie使用,仅供学习交流,Python)

这个程序是一个用于下载B站充电视频的工具,依赖于用户提供的会员Cookies。

qq_40753021的博客 2万+

go Exercise: Equivalent Binary Trees

Implement the Walk function. Test the Walk function. The function tree.New(k) constructs a randomly-structured (but always sorted) binary tree holding the values k, 2k, 3k, …, 10k. Create a new channel ch and kick off the walker: go Walk(tree.New(1),..

weixin_42476669的博客 378

在Termux上安装Kali Linux 完整系统

介绍如何在Termux上安装Kali Linux 完整系统

蓑雨一生的博客 2万+

YOLOv8【第十一章:视频追踪与流处理篇·第3节】匈牙利算法(Hungarian Algorithm)与 KM 算法匹配机制详解!

🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏。该专栏系统复现并梳理全网各类 YOLOv8 改进与实战案例(当前已覆盖分类 / 检测 / 分割 / 追踪 / 关键点 / OBB 检测等方向),坚持持续更新 + 深度解析,质量分长期稳定在 97 分以上,可视为当前市面上 覆盖较全、更新较快、实战导向极强 的 YOLO 改进系列内容之一。

**My Coding Family** 444

【GoLang笔记】A Tour of Go - Exercise: Equivalent Binary Trees

本文是GoLang学习教程中的一道习题,具体题目要求及代码实现如下。 Exercise: Equivalent Binary Trees 1. Implement the Walk function. 2. Test the Walk function. The function tree.New(k) constructs a randomly-structured binary t

slvher的专栏 2088

【LE Audio】CAP精讲[1]: 从理论到实操,CAP 协同流程入门全攻略

LEAudio生态中的CommonAudioProfile(CAP)是一个关键协同框架,它通过整合基础音频协议(BAP/VCP/MICP等)构建多设备协同能力。CAP定义了三大核心功能:音频流管理、设备组控制和统一交互服务,解决了传统蓝牙音频在多设备场景下的协同痛点。其规范历经v1.0到v1.0.1的优化,修正了角色定义、连接流程等关键模块,提升设备兼容性。CAP采用严格的语言约定(shall/must/should)和术语体系(ContextType/CCID/协同集),确保不同厂商设备能相互理解。

byte轻骑兵的技术小窝 2万+

go exercise stringers

GO 官方文档习题 Exercise: Stringers Make the IPAddr type implement fmt.Stringer to print the address as a dotted quad. For instance, IPAddr{1, 2, 3, 4} should print as “1.2.3.4”. package main import "fmt" type IPAddr [4]byte // TODO: Add a "String() string" m

weixin_42476669的博客 500

Graphviz安装及使用:决策树可视化

Python大数据分析与机器学习学习笔记 Graphviz安装及使用:决策树可视化

算法工程师 1万+

【3天快速入门WPF】从小白到新手入门教程

嘿,小伙伴们!如果你已经有一定的C#开发基础,但想快速掌握WPF开发,达到企业级水准,那接下来的这个三天快速入门计划绝对适合你!虽然听起来有点挑战,但别担心,只要跟着这个高强度、结构化的学习计划,你一定能搞定它。首先,我们得搞清楚,WPF的核心知识点到底有哪些。要想达到企业级开发水准,XAML、数据绑定、MVVM、控件使用、命令系统、样式模板这些统统都得拿下。听起来是不是有点多?别急,我们把任务分解到三天里,每天安排合理的内容,保证理论和实践相结合,让你学得扎实又高效。

Zy100Papa的博客 1422

Exercise: Stringers

Make the IPAddr type implement fmt.Stringer to print the address as a dotted quad. For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4". 1 package main 2 3 import "fmt" 4 5 ...

weixin_33785108的博客 291

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【笔记】A Tour of Go - 练习篇

本文总结了 Go Tour 中各个练习的实现,语言特性总结于另一篇文章中。

秋叶红于二月花 1047

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Go语言学习笔记-A Tour of Go 练习笔记-Equivalent Binary Trees

GO语言学习笔记之实战训练集-11

sxmatch的技术人生 360

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

A Tour of Go : Exercise: Maps

A Tour of Go系列。如有问题欢迎指出~ Tour第二篇,直接贴代码吧,同样很简单。 1 package main 2 3 import ( 4 "code.google.com/p/go-tour/wc" 5 "strings" 6 ) 7 8 func WordCount(s string) map[string]int {...

weixin_30892763的博客 156

golang channel 使用方法

Golang Channel 1. A goroutine is a lightweight thread managed by the Go runtime. go f(x, y, z) starts a new goroutine running f(x, y, z) The evaluation of f, x, y, and z happens in the current gorou...

浴血重生-学习空间 975

C#LeetCode刷题之#100-相同的树(Same Tree)

问题 给定两个二叉树,编写一个函数来检验它们是否相同。 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] 输出: true 输入: 1 ...

比特飞 1万+

[Go语言]我的第五个Go语言程序

Exercise: Stringers Make the IPAddr type implementfmt.Stringer to print the address as a dotted quad. For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4". package main import "fmt"

zjysource的专栏 749

《Go 指南》 练习:Web 爬虫 || A Tour of Go , Exercise: Web Crawler

练习地址: Go指南 关键代码段: 注意 wg.Done 的位置不能放到 Crawl 方法中 sc.wg.Add(1) 后面,否则会出问题。【参考链接中的规则一】 Unlock同理 type SafeCounter struct { mp map[string]bool mux sync.Mutex wg sync.WaitGroup } // Crawl 使用 fetcher 从某个 URL 开始递归的爬取页面,直到达到最大深度。 func (sc *SafeCounter)C.

米兰的小科比 695

练习:Stringers

练习:Stringers 让 IPAddr 类型实现 fmt.Stringer 以便用点分格式输出地址。 例如,`IPAddr{1,`2,`3,`4}` 应当输出 `"1.2.3.4"`。 package main import "fmt" type IPAddr [4]byte // TODO: Add a "String() string" method to IPAd

chen 2717
上一篇: A Tour of Go: Exercise: Rot13 Reader
下一篇: A Tour of Go - Exercise: Web Crawler
帅帅兔子
博客等级 码龄24年 7粉丝 29原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅帅兔子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值