package main import "fmt" import "golang.org/x/tour/tree" func recWalk(t *tree.Tree, ch chan int) { if t.Left != nil { recWalk(t.Left,ch) } ch <- t.Value if t.Right != nil { recWalk(t.Right,ch) } } // Walk walks the tree t sending all values // from the tree to the channel ch. func Walk(t *tree.Tree, ch chan int) { recWalk(t,ch) close(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) for { v1, ok1 := <-ch1 v2, ok2 := <-ch2 if !ok1 && !ok2 { break } if ok1 != ok2 { return false } if v1 != v2 { return false } } return true } func main() { ch := make(chan int) go Walk(tree.New(1), ch) for i := range ch { fmt.Println(i) } fmt.Println(Same(tree.New(1), tree.New(1))) fmt.Println(Same(tree.New(1), tree.New(2))) }