Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "golang.org/x/tour/tree"
- import "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 {
- return
- }
- ch <- t.Value
- Walk(t.Left, ch)
- 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)
- go func() {
- Walk(t1, ch1)
- close(ch1)
- }()
- ch2 := make(chan int)
- go func() {
- Walk(t2, ch2)
- close(ch2)
- }()
- var arr1 []int
- for elem := range ch1 {
- arr1 = append(arr1, elem)
- }
- var arr2 []int
- for elem := range ch2 {
- arr2 = append(arr2, elem)
- }
- for i, _ := range arr1 {
- if arr1[i] != arr2[i] {
- return false
- }
- }
- return true
- }
- func main() {
- ch1 := make(chan int)
- go func() {
- Walk(tree.New(1), ch1)
- close(ch1)
- }()
- a := tree.New(1)
- fmt.Println("Done %s", Same(a,tree.New(1)))
- }
Add Comment
Please, Sign In to add comment