Skip to content

Instantly share code, notes, and snippets.

@kaen98
Last active May 6, 2020 01:56
Show Gist options
  • Save kaen98/ae6102fd38298471471575af73cb7afa to your computer and use it in GitHub Desktop.
Save kaen98/ae6102fd38298471471575af73cb7afa to your computer and use it in GitHub Desktop.
Exercise: Equivalent Binary Trees
package main
import (
"golang.org/x/tour/tree"
"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 != nil {
Walk(t.Left, ch)
ch <- t.Value
Walk(t.Right, ch)
}
}
func goWalk(t *tree.Tree, ch chan int) {
Walk(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, 100)
ch2 := make(chan int, 100)
go goWalk(t1, ch1)
go goWalk(t2, ch2)
for {
v1, ok1 := <-ch1
v2, ok2 := <-ch2
if (ok1 != ok2 || v1 != v2) {
return false
}
if (ok1 == false) {
break;
}
}
return true
}
func main() {
fmt.Println(Same(tree.New(1), tree.New(1)))
fmt.Println(Same(tree.New(1), tree.New(2)))
fmt.Println(Same(tree.New(2), tree.New(2)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment