Skip to content

Instantly share code, notes, and snippets.

@matipan
Created November 21, 2015 19:45
Show Gist options
  • Save matipan/8c9a89e27cbef6b3e16b to your computer and use it in GitHub Desktop.
Save matipan/8c9a89e27cbef6b3e16b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"golang.org/x/tour/tree"
)
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 GoWalk(t *tree.Tree) chan int {
ch := make(chan int)
go func() {
Walk(t, ch)
close(ch)
}()
return ch
}
func Same(t1, t2 *tree.Tree) bool {
c1, c2 := GoWalk(t1), GoWalk(t2)
for {
i, done_1 := <-c1
n, done_2 := <-c2
if !done_1 || !done_2 {
// if we are done with both channels then they have the same values
return done_1 == done_2
}
if i != n {
break
}
}
return false
}
func main() {
fmt.Println(Same(tree.New(1), tree.New(1)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment