Created
July 27, 2018 10:51
-
-
Save dulltz/e1e2e4087cbe425aacdbf73baa90e94e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "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.Left != nil { | |
| Walk(t.Left, ch) | |
| } | |
| if t != nil { | |
| ch <- t.Value | |
| } | |
| if t.Right != nil { | |
| Walk(t.Right, ch) | |
| } | |
| } | |
| // Same determines whether the trees | |
| // t1 and t2 contain the same values. | |
| func Same(t1, t2 *tree.Tree) bool { | |
| c1 := make(chan int) | |
| c2 := make(chan int) | |
| go func() { | |
| Walk(t1, c1) | |
| close(c1) | |
| }() | |
| go func() { | |
| Walk(t2, c2) | |
| close(c2) | |
| }() | |
| for v := range c1 { | |
| if v != <-c2 { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| 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