Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. package main
  2.  
  3. import "golang.org/x/tour/tree"
  4. import "fmt"
  5.  
  6.  
  7. func Walk(t *tree.Tree, ch chan int) {
  8. walkRecursive(t, ch)
  9. close(ch)
  10. }
  11.  
  12. func walkRecursive(t *tree.Tree, ch chan int) {
  13. ch <- t.Value
  14. if t.Left != nil {
  15. walkRecursive(t.Left, ch)
  16. }
  17. if t.Right != nil {
  18. walkRecursive(t.Right, ch)
  19. }
  20. }
  21.  
  22. func Same(t1, t2 *tree.Tree) bool {
  23. ch1 := make(chan int)
  24. go Walk(t1, ch1)
  25. for checkVal := range ch1 {
  26. found := false
  27. ch2 := make(chan int)
  28. go Walk(t2, ch2)
  29. for checkVal2 := range ch2 {
  30. if checkVal == checkVal2 {
  31. found = true
  32. break
  33. }
  34. }
  35. if !found {
  36. return false
  37. }
  38. }
  39. return true
  40. }
  41.  
  42. func main() {
  43. t1 := tree.New(2)
  44. t2 := tree.New(1)
  45.  
  46. res := Same(t1, t2)
  47. fmt.Printf("Result: %v \n", res)
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement