title: Go Tour Exercise #7: Binary Trees equivalence
body: <p>I am trying to solve <a href="https://tour.golang.org/concurrency/7" rel="noreferrer">equivalent binary trees</a> exercise on go tour. Here is what I did;</p>
  <pre><code>package main
  
  import &quot;tour/tree&quot;
  import &quot;fmt&quot;
  
  // 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)
      }
      ch &lt;- 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 {
      ch1 := make(chan int)
      ch2 := make(chan int)
      go Walk(t1, ch1)
      go Walk(t2, ch2)
      for k := range ch1 {
          select {
          case g := &lt;-ch2:
              if k != g {
                  return false
              }
          default:
              break
          }
      }
      return true
  }
  
  func main() {
      fmt.Println(Same(tree.New(1), tree.New(1)))
      fmt.Println(Same(tree.New(1), tree.New(2)))
  }
  </code></pre>
  <p>However, I couldn't find out how to signal if any no more elements left in trees. I can't use <code>close(ch)</code> on <code>Walk()</code> because it makes the channel close before all values are sent (because of recursion.) Can anyone lend me a hand here?</p>