Skip to content

Instantly share code, notes, and snippets.

@d-baranowski
Created December 13, 2019 13:58
Show Gist options
  • Save d-baranowski/4ea946e9be11fd4e988eaffbd559ecda to your computer and use it in GitHub Desktop.
Save d-baranowski/4ea946e9be11fd4e988eaffbd559ecda to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"golang.org/x/tour/tree"
)
func main() {
fmt.Println("Tour of Go Solution - Equivalent Binary Trees\n")
t1 := tree.New(5)
t2 := tree.New(5)
fmt.Println("Tree 1:", t1)
fmt.Println("Tree 2:", t2)
fmt.Println("Are they same? - ", Same(t1, t2))
fmt.Println("------")
t3 := tree.New(2)
t4 := tree.New(3)
fmt.Println("Tree 3:", t3)
fmt.Println("Tree 4:", t4)
fmt.Println("Are they same? - ", Same(t3, t4))
fmt.Println("")
}
func Walker(t *tree.Tree, ch chan int) {
Walk(t, ch)
close(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)
}
}
func Same(t1, t2 *tree.Tree) bool {
c1 := make(chan int)
c2 := make(chan int)
go Walker(t1, c1)
go Walker(t2, c2)
for i := range c1 {
if i != <-c2 {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment