Go Tour Exercise: Equivalent Binary Trees

前端 未结 23 1786
攒了一身酷
攒了一身酷 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:30

    Tried to solve this problem using map structure.

    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
    }
    

提交回复
热议问题