Go 巡更练习:等价二叉树

Go 巡更练习:等价二叉树,go,Go,我正在努力解决巡回演出中的锻炼问题。这就是我所做的 package main import "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.Left != nil { Walk(t.Left, ch) }

我正在努力解决巡回演出中的锻炼问题。这就是我所做的

package main

import "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.Left != nil {
        Walk(t.Left, ch)
    }
    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 {
    ch1 := make(chan int)
    ch2 := make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)
    for k := range ch1 {
        select {
        case g := <-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)))
}
主程序包
导入“巡更/树”
输入“fmt”
//Walk在树上行走,并发送所有值
//从树到通道ch。
func Walk(t*tree.tree,ch chan int){
如果t.左!=零{
步行(t.左,ch)
}
ch如果Walk函数本身不递归,则可以使用close()。例如,Walk只需执行以下操作:

func Walk(t *tree.Tree, ch chan int) {
    walkRecurse(t, ch)
    close(ch)
}
其中walkRecurse或多或少是您当前的Walk函数,但在walkRecurse上递归。 (或者您将Walk重写为迭代式——当然,这更为模糊) 使用这种方法,您的()函数必须了解通道是,这是通过窗体的通道接收完成的

k, ok1 := <-ch
g, ok2 := <-ch
您必须实现countTreeNodes()函数,该函数应计算*树中的节点数

func Walk(t*tree.tree,ch chan int){

延迟关闭(ch)/这是我的解决方案。它正确地检查两个序列的长度差异

package main

import "code.google.com/p/go-tour/tree"
import "fmt"

func Walk(t *tree.Tree, ch chan int) {
    var walker func (t *tree.Tree)
    walker = func (t *tree.Tree) {
        if t.Left != nil {
            walker(t.Left)
        }
        ch <- t.Value
        if t.Right != nil {
            walker(t.Right)
        }
    }
    walker(t)
    close(ch)
}

func Same(t1, t2 *tree.Tree) bool {
    chana := make (chan int)
    chanb := make (chan int)

    go Walk(t1, chana)
    go Walk(t2, chanb)

    for {
        n1, ok1 := <-chana
        n2, ok2 := <-chanb        
        if n1 != n2 || ok1 != ok2 {
            return false
        }
        if (!ok1) {
            break
        }
    }
    return true; 
}
主程序包
导入“code.google.com/p/go-tour/tree”
输入“fmt”
func Walk(t*tree.tree,ch chan int){
var walker func(t*tree.tree)
walker=func(t*tree.tree){
如果t.左!=零{
沃克(t.左)
}

ch您几乎完全正确,不需要使用
select
语句,因为您将经常遇到
default
情况,下面是我的解决方案,它不需要计算树中的节点数:

func Same(t1, t2 *tree.Tree) bool {
    ch1, ch2 := make(chan int), make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)
    for i := range ch1 {
        j, more := <-ch2
        if more {
            if i != j { return false }
        } else { return false }
    }

    return true
}
func-Same(t1,t2*tree.tree)bool{
ch1,ch2:=make(chan int),make(chan int)
步行(t1,ch1)
步行(t2,ch2)
对于i:=范围ch1{

j、 更多:=以下是使用此处和线程中的想法的完整解决方案

主程序包
输入“fmt”
导入“code.google.com/p/go-tour/tree”
//Walk在树上行走,并发送所有值
//从树到通道ch。
func Walk(t*tree.tree,ch chan int){
var walker func(t*tree.tree)
walker=func(t*tree.tree){
如果(t==nil){
返回
}
沃克(t.左)

ch以下是我提出的解决方案:

func Walker(t *tree.Tree, ch chan int){
    if t==nil {return}
    Walker(t.Left,ch)
    ch<-t.Value
    Walker(t.Right,ch)   
}

func Walk(t *tree.Tree, ch chan int){
   Walker(t,ch)
   close(ch)
}

func Same(t1, t2 *tree.Tree) bool{
    ch:=make(chan int)
    dh:=make(chan int)
    go Walk(t1,ch)
    go Walk(t2,dh)

    for i:=range ch {
        j,ok:=<-dh
        if(i!=j||!ok)  {return false} 
    }

    return true
}
func Walker(t*tree.tree,ch chan int){
如果t==nil{return}
沃克(t.左,ch)

ch您应该避免让打开的通道无人值守,否则线程可能会永远等待,永远不会结束

package main

import "code.google.com/p/go-tour/tree"
import "fmt"

func WalkRecurse(t *tree.Tree, ch chan int) {
    if t == nil {
        return
    }

    WalkRecurse(t.Left, ch)
    ch <- t.Value
    WalkRecurse(t.Right, ch)
}

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
    WalkRecurse(t, ch)
    close(ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    var ch1, ch2 chan int = make(chan int), make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)

    ret := true
    for {
        v1, ok1 := <- ch1
        v2, ok2 := <- ch2

        if ok1 != ok2 {
            ret = false
        }
        if ok1 && (v1 != v2) {
            ret = false
        }
        if !ok1 && !ok2 {
            break
        }
    }

    return ret
}

func main() {
    ch := make(chan int)
    go Walk(tree.New(1), ch)
    for v := range ch {
        fmt.Print(v, " ")
    }
    fmt.Println()

    fmt.Println(Same(tree.New(1), tree.New(1)))
    fmt.Println(Same(tree.New(1), tree.New(2)))
}
主程序包
导入“code.google.com/p/go-tour/tree”
输入“fmt”
func WalkRecurse(t*tree.tree,ch chan int){
如果t==nil{
返回
}
步进递归(t.左,ch)
我的版本

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 WalkRec(t *tree.Tree, ch chan int) {
    if t == nil {
        return
    }
    WalkRec(t.Left, ch)
    ch <- t.Value
    WalkRec(t.Right, ch)
}

func Walk(t *tree.Tree, ch chan int) {
    WalkRec(t, ch)
    close(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 {
        x, okx := <-ch1
        y, oky := <-ch2
        switch {
        case okx != oky:
            return false
        case x != y:
            return false
        case okx == oky && okx == false:
            return true
        }

    }

}

func main() {
    ch := make(chan int)
    go Walk(tree.New(1), ch)
    fmt.Println(Same(tree.New(1), tree.New(1)))
    fmt.Println(Same(tree.New(2), tree.New(1)))
    fmt.Println(Same(tree.New(1), tree.New(2)))
}
主程序包
进口(
“fmt”
“golang.org/x/tour/tree”
)
//Walk在树上行走,并发送所有值
//从树到通道ch。
func WalkRec(t*tree.tree,ch chan int){
如果t==nil{
返回
}
WalkRec(t.左,ch)

ch我写了两个版本,始终将两个通道读到底:

package main

import (
    "fmt"
    "golang.org/x/tour/tree"
)

func Walk(t *tree.Tree, ch chan int) {
    var walker func(t *tree.Tree)
    walker = func(t *tree.Tree) {
        if t == nil {
            return
        }
        walker(t.Left)
        ch <- t.Value
        walker(t.Right)
    }
    walker(t)
    close(ch)
}

func Same(t1, t2 *tree.Tree, sameChan func(ch1, ch2 chan int) bool) bool {
    ch1, ch2 := make(chan int), make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)

    return sameChan(ch1, ch2)
}

func sameChan1(ch1, ch2 chan int) bool {
    areSame := true
    for {
        v1, ok1 := <-ch1
        v2, ok2 := <-ch2

        if !ok1 && !ok2 {
            return areSame
        }

        if !ok1 || !ok2 || v1 != v2 {
            areSame = false
        }
    }
}

func sameChan2(ch1, ch2 chan int) bool {
    areSame := true
    for v1 := range ch1 {
        v2, ok2 := <-ch2

        if !ok2 || v1 != v2 {
            areSame = false
        }
    }
    for _ = range ch2 {
        areSame = false
    }
    return areSame
}

func main() {
    fmt.Println(Same(tree.New(1), tree.New(1), sameChan1))
    fmt.Println(Same(tree.New(2), tree.New(1), sameChan1))
    fmt.Println(Same(tree.New(1), tree.New(2), sameChan1))

    fmt.Println(Same(tree.New(1), tree.New(1), sameChan2))
    fmt.Println(Same(tree.New(2), tree.New(1), sameChan2))
    fmt.Println(Same(tree.New(1), tree.New(2), sameChan2))
}
主程序包
进口(
“fmt”
“golang.org/x/tour/tree”
)
func Walk(t*tree.tree,ch chan int){
var walker func(t*tree.tree)
walker=func(t*tree.tree){
如果t==nil{
返回
}
沃克(t.左)

ch我就是这样使用顺序遍历的

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 != nil {
        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 {
    c1, c2 := make(chan int), make(chan int)
    go Walk(t1, c1)
    go Walk(t2, c2)
    if <-c1 == <-c2 {
        return true
    } else {
        return false
    }
}

func main() {
    t1 := tree.New(1)
    t2 := tree.New(8)
    fmt.Println("the two trees are same?", Same(t1, t2))
}
主程序包
进口(
“fmt”
“golang.org/x/tour/tree”
)
//Walk在树上行走,并发送所有值
//从树到通道ch。
func Walk(t*tree.tree,ch chan int){
如果t!=nil{
步行(t.左,ch)

ch因为问题只是说树只有10个节点,那么下面是我在阅读其他答案后的答案:

func Walk(t *tree.Tree, ch chan int) {
    defer close(ch)

    var walker func(t *tree.Tree)
    walker = func(t *tree.Tree) {
        if t == nil {
            return
        }

        walker(t.Left)
        ch <- t.Value
        walker(t.Right)
    }
    walker(t)
}

func Same(t1, t2 *tree.Tree) bool {
    ch1, ch2 := make(chan int), make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)

    for range make([]struct{}, 10) {
        if <-ch1 != <-ch2 {
            return false
        }
    }
    return true
}
func Walk(t*tree.tree,ch chan int){
延迟关闭(ch)
var walker func(t*tree.tree)
walker=func(t*tree.tree){
如果t==nil{
返回
}
沃克(t.左)

ch这里的解决方案不依赖于不同的树长度,也不依赖于遍历顺序:

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) {
    var walk func(*tree.Tree)
    walk = func(tr *tree.Tree) {
        if tr == nil {
            return
        }

        walk(tr.Left)
        ch <- tr.Value
        walk(tr.Right)
    }

    walk(t)
    close(ch)
}

func merge(ch chan int, m map[int]int) {
    for i := range ch {
        count, ok := m[i]
        if ok {
            m[i] = count + 1
        } else {
            m[i] = 1
        }
    }
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    ch1 := make(chan int, 100)
    ch2 := make(chan int, 100)
    m := make(map[int]int)

    go Walk(t1, ch1)
    go Walk(t2, ch2)

    merge(ch1, m)
    merge(ch2, m)

    for _, count := range m {
        if count != 2 {
            return false
        }
    }

    return true
}
主程序包
进口(
“fmt”
“golang.org/x/tour/tree”
)
//Walk在树上行走,并发送所有值
//从树到通道ch。
func Walk(t*tree.tree,ch chan int){
var walk func(*tree.tree)
walk=func(tr*tree.tree){
如果tr==nil{
返回
}
步行(左)

ch我就是这样做的,不同的是你可以将
Walk
包装到匿名函数中,并在匿名函数中使用
defer-close(ch)
。因此你不需要定义其他命名的递归函数

package main

import (
    "golang.org/x/tour/tree"
    "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
    }
    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, ch2 := make(chan int), make(chan int)
    go func() {
        defer close(ch1)
        Walk(t1, ch1)
    }()
    go func() {
        defer close(ch2)
        Walk(t2, ch2)
    }()
    for {
        v1, ok1 := <- ch1
        v2, ok2 := <- ch2
        if ok1 != ok2 || v1 != v2 {
            return false
        }
        if !ok1 && !ok2 {
            break
        }
    }
    return true
}

func main() {
    ch := make(chan int)
    go func () {
        defer close(ch)
        Walk(tree.New(3), ch)
    }()
    for i := range ch {
        fmt.Println(i)
    }

    fmt.Println(Same(tree.New(1), tree.New(1)))
    fmt.Println(Same(tree.New(1), tree.New(2)))
    fmt.Println(Same(tree.New(10), tree.New(10)))
}
主程序包
进口(
“golang.org/x/tour/tree”
“fmt”
)
//Walk在树上行走,并发送所有值
//从树到通道ch。
func Walk(t*tree.tree,ch chan int){
如果t==nil{
返回
}
步行(t.左,ch)

ch对于任何感兴趣的人,如果您想知道如何在不创建单独递归函数的情况下解决此问题,下面是使用堆栈的答案:

func Walk(t *tree.Tree, ch chan int) {
    defer close(ch)
    visitStack := []*tree.Tree{t}
    visited := make(map[*tree.Tree]bool, 1)
    for len(visitStack) > 0 {
        var n *tree.Tree
        n, visitStack = visitStack[len(visitStack)-1], visitStack[:len(visitStack)-1]
        if visited[n] {
            ch <- n.Value
            continue
        }
        if n.Right != nil {
            visitStack = append(visitStack, n.Right)
        }
        visitStack = append(visitStack, n)
        if n.Left != nil {
            visitStack = append(visitStack, n.Left)
        }
        visited[n] = true
    }
}
func Walk(t*tree.tree,ch chan int){
延迟关闭(ch)
visitStack:=[]*树.树{t}
已访问:=make(映射[*tree.tree]bool,1)
对于len(visitStack)>0{
变量n*tree.tree
n、 visitStack=visitStack[len(visitStack)-1],visitStack[:len(visitStack)-1]
如果访问[n]{
ch一个明确的答案:

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
    }
    Walk(t.Left, ch)
    ch <- t.Value
    Walk(t.Right, ch)
}

func WalkATree(t *tree.Tree, ch chan int) {
    Walk(t, ch)
    close(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 WalkATree(t1, ch1)
    go WalkATree(t2, ch2)
    var v1, v2 int
    var ok1, ok2 bool
    for {
        v1, ok1 = <- ch1
        v2, ok2 = <- ch2
        if !ok1 && !ok2 {
            return true
        }
        if !ok1 && ok2 || ok1 && !ok2 {
            return false
        }
        if v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Println(Same(tree.New(1), tree.New(1)))
}
主程序包
导入“golang.org/x/tour/tree”
输入“fmt”
//Walk在树上行走,并发送所有值
//从树到通道ch。
func Walk(t*tree.tree,ch chan int){
如果t==nil{
返回
}
步行(t.左,ch)

ch试图用map结构来解决这个问题

func Same(t1, t2 *tree.Tree) bool {
    countMap := make(map[int]int)
    ch := make(chan int)
    go Walk(t1, ch)
    for v := range ch {
        countMap[v]++
    }
    ch = make(chan int)
    go Walk(t2, ch)
    for v := range ch {
        countMap[v]--
        if countMap[v] < 0 {
            return false
        }
    }
    return true
}
func-Same(t1,t2*tree.tree)bool{
countMap:=make(map[int]int)
ch:=制造(成交量)
步行(t1,ch)
对于v:=范围ch{
计数图[v]++
}
ch=制造(成交量)
步行(t2,ch)
对于v:=范围ch{
计数图[v]--
如果countMap[v]<0{
返回错误
}
}
返回真值
}

而我的第一个直觉是也将递归遍历包装起来
func Walk(t *tree.Tree, ch chan int) {
    defer close(ch)
    visitStack := []*tree.Tree{t}
    visited := make(map[*tree.Tree]bool, 1)
    for len(visitStack) > 0 {
        var n *tree.Tree
        n, visitStack = visitStack[len(visitStack)-1], visitStack[:len(visitStack)-1]
        if visited[n] {
            ch <- n.Value
            continue
        }
        if n.Right != nil {
            visitStack = append(visitStack, n.Right)
        }
        visitStack = append(visitStack, n)
        if n.Left != nil {
            visitStack = append(visitStack, n.Left)
        }
        visited[n] = true
    }
}
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
    }
    Walk(t.Left, ch)
    ch <- t.Value
    Walk(t.Right, ch)
}

func WalkATree(t *tree.Tree, ch chan int) {
    Walk(t, ch)
    close(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 WalkATree(t1, ch1)
    go WalkATree(t2, ch2)
    var v1, v2 int
    var ok1, ok2 bool
    for {
        v1, ok1 = <- ch1
        v2, ok2 = <- ch2
        if !ok1 && !ok2 {
            return true
        }
        if !ok1 && ok2 || ok1 && !ok2 {
            return false
        }
        if v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Println(Same(tree.New(1), tree.New(1)))
}
func Same(t1, t2 *tree.Tree) bool {
    countMap := make(map[int]int)
    ch := make(chan int)
    go Walk(t1, ch)
    for v := range ch {
        countMap[v]++
    }
    ch = make(chan int)
    go Walk(t2, ch)
    for v := range ch {
        countMap[v]--
        if countMap[v] < 0 {
            return false
        }
    }
    return true
}
package main

import (
    "fmt"
    "golang.org/x/tour/tree"
)

func Walk(t *tree.Tree, ch chan int) {
    if t.Left != nil {
        Walk(t.Left, ch)
    }
    ch <- t.Value
    if t.Right != nil {
        Walk(t.Right, ch)
    }
}

func Same(t1, t2 *tree.Tree) bool {
    ch1 := make(chan int)
    ch2 := make(chan int)

    defer close(ch1)
    defer close(ch2)

    go Walk(t1, ch1)
    go Walk(t2, ch2)

    for i := 0; i < 10; i++ {
        if <-ch1 != <-ch2 {
            return false
        }
    }

    return true
}

func main() {
    fmt.Println(Same(tree.New(1), tree.New(2)))
}
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same2(t1, t2 *tree.Tree) bool
fmt.Println("Should return true:", Same(tree.New(1), tree.New(1)))
fmt.Println("Should return false:", Same(tree.New(1), tree.New(2)))
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    ch1, ch2 := make(chan int), make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)

    var tv1 = []int{}

    for v := range ch1 {
        tv1 = append(tv1, v)
    }

    inArray := func(arr []int, value int) bool {
        for a := range arr {
            if arr[a] == value {
                return true
            }
        }
        return false
    }

    for v2 := range ch2 {
        if !inArray(tv1, v2) {
            return false
        }
    }

    return true
}
package main

import (
    "golang.org/x/tour/tree"
    "fmt"
)

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
    walkRecursive(t, ch)
    close(ch)
}

func walkRecursive(t *tree.Tree, ch chan int) {
    if t != nil {
        walkRecursive(t.Left, ch)
        ch <- t.Value
        walkRecursive(t.Right, ch)
    }
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    var br bool
    ch1, ch2 := make(chan int), make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)

    for i:= range ch1 {
        if i == <-ch2 {
            br = true
        } else {
            br = false
            break
        }
    }
    return br
}

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

    for i := range ch {
        fmt.Println(i)
    }

    fmt.Println(Same(tree.New(1), tree.New(2)))
    fmt.Println(Same(tree.New(1), tree.New(1)))
    fmt.Println(Same(tree.New(2), tree.New(1)))
}
1
2
3
4
5
6
7
8
9
10
false
true
false
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 != nil {
        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, ch2 := make(chan int), make(chan int)
    go func() { Walk(t1, ch1); close(ch1) }()
    go func() { Walk(t2, ch2); close(ch2) }()
    for v1 := range ch1 {
        if v1 != <-ch2 {
            return false
        }
    }
    return true
}

func main() {
    fmt.Println(Same(tree.New(1), tree.New(1)))
    fmt.Println(Same(tree.New(2), tree.New(1)))
}
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) {
    defer close(ch)
    var iw func(*tree.Tree)
    iw = func(it *tree.Tree) {
        if it == nil {
            return
        }
        iw(it.Left)
        ch <- it.Value
        iw(it.Right)
    }
    iw(t)
}

// 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, more1 := <- ch1
        v2, more2 := <- ch2
        if (!more1 && !more2) {
            return true
        }
        if more1 != more2 || v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Println(Same(tree.New(1), tree.New(1)))
    fmt.Println(Same(tree.New(1), tree.New(2)))
}
package main

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

func Pop(stack []*tree.Tree) (*tree.Tree, []*tree.Tree) {
    last := len(stack) - 1
    node := stack[last]
    stack[last] = nil
    return node, stack[:last]
}

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
    defer close(ch)
    stack := []*tree.Tree{t}
    var node *tree.Tree
    for len(stack) > 0 {
        node, stack = Pop(stack)
        if node.Left != nil {
            stack = append(stack, &tree.Tree{nil, node.Value, node.Right}, node.Left)
            continue
        }

        ch <- node.Value

        if node.Right != nil {
            stack = append(stack, node.Right)
        }
    }
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    ch1, ch2 := make(chan int), make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)
    for {
        v1, ok1 := <-ch1
        v2, ok2 := <-ch2
        if v1 != v2 {
            return false
        }
        if !ok1 || !ok2 {
            return ok1 == ok2
        }
    }
}

func PrintTree(t *tree.Tree) {
    ch := make(chan int)
    go Walk(t, ch)
    for i := range ch {
        fmt.Printf("%d ", i)
    }
    fmt.Println()
}

func main() {
    PrintTree(tree.New(1))
    PrintTree(&tree.Tree{Value: 1, Right: &tree.Tree{Value: 2}})

    fmt.Println("1 and 2 same (false): ", Same(tree.New(1), tree.New(2)))
    fmt.Println("1 and 1 same (true): ", Same(tree.New(1), tree.New(1)))
    fmt.Println("empty same (true): ", Same(&tree.Tree{}, &tree.Tree{}))
    fmt.Println("diff length same (false): ", Same(&tree.Tree{Value: 1}, &tree.Tree{Value: 2, Left: &tree.Tree{Value: 2}}))
}
1 2 3 4 5 6 7 8 9 10 
1 2 
1 and 2 same (false):  false
1 and 1 same (true):  true
empty same (true):  true
diff length same (false):  false