Go Tour Exercise: Equivalent Binary Trees

前端 未结 23 1636
攒了一身酷
攒了一身酷 2020-12-12 23:39

I am trying to solve equivalent binary trees exercise on go tour. Here is what I did;

package main

import \"tour/tree\"
import \"fmt\"

// Walk walks the tr         


        
23条回答
  •  囚心锁ツ
    2020-12-13 00:33

    Here's the full solution using ideas here and from the Google Group thread

    package main
    
    import "fmt"
    import "code.google.com/p/go-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 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)
    }
    
    // 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 || ok1 != ok2 {
                return false
            }
    
            if !ok1 {
                break
            }
        }
    
        return true
    }
    
    func main() {
        fmt.Println("1 and 1 same: ", Same(tree.New(1), tree.New(1)))
        fmt.Println("1 and 2 same: ", Same(tree.New(1), tree.New(2)))
    
    }
    

提交回复
热议问题