A Tour of Go Exercise: Equivalent Binary Trees
模範解答かわからないけど解いてみたのでメモしておく。 Exercise: Equivalent Binary Trees
package main import "golang.org/x/tour/tree" import "fmt" 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)} } func Same(t1, t2 *tree.Tree) bool { fmt.Println(t1) fmt.Println(t2) ch1 := make(chan int, 10) ch2 := make(chan int, 10) go func() {Walk(t1, ch1); close(ch1)}() go func() {Walk(t2, ch2); close(ch2)}() for item := range ch1 { if item != <-ch2 {return false} } for _ = range ch2 {return false} return true } func main() { fmt.Println(Same(tree.New(2),tree.New(2))) }