Skip to content

Commit

Permalink
add first version
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoseangel committed Aug 26, 2024
1 parent 1382778 commit dc7b781
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go/exercises/equivalentbinarytrees/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module main

go 1.23.0

require golang.org/x/tour v0.1.0 // indirect
2 changes: 2 additions & 0 deletions go/exercises/equivalentbinarytrees/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE=
golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc=
44 changes: 44 additions & 0 deletions go/exercises/equivalentbinarytrees/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import "golang.org/x/tour/tree"

// 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 {
return
}

Walk(t.Left, ch)
ch <- t.Value
Walk(t.Right, 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 v1 != v2 || ok1 != ok2 {
return false
}
if !ok1 {
break
}
}
return true
}

func main() {
ch := make(chan int)
go Walk(tree.New(3), ch)

for v := range ch {
println(v)
}
}

0 comments on commit dc7b781

Please sign in to comment.