做微网站,4399小游戏大全,全网营销英文,中国建筑集团网站goroutine
http://127.0.0.1:3999/concurrency/8
question
使用 go 判断 两个 树 存放的 序列 是否 相同, 如果 相同 他们 被称为 equivalent_tree
tree struct type Tree struct {Left *TreeValue intRight *Tree
}由于 递归的 写法 比较简单, 此处 使用循环的 形式 来实…goroutine
http://127.0.0.1:3999/concurrency/8
question
使用 go 判断 两个 树 存放的 序列 是否 相同, 如果 相同 他们 被称为 equivalent_tree
tree struct type Tree struct {Left *TreeValue intRight *Tree
}
由于 递归的 写法 比较简单, 此处 使用循环的 形式 来实现. 因为 循环的 写法 不存在 调用栈的 瓶颈, 所以 在实际工程中, 能够 避免 递归 还是 避免递归吧. judge tree
package mainimport 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) {_ts : make(chan *tree.Tree, 100)_ts - t
OuterLoop:for {_f1 : func(_tmp *tree.Tree) {fmt.Println(_tmp.Value)ch - _tmp.Valuefor _, v : range []*tree.Tree{_tmp.Left, _tmp.Right} {if v ! nil {_ts - v}}}select {case _tmp : -_ts:_f1(_tmp)default:fmt.Println(break)break OuterLoop}}
}// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {return true
}func main() {_t1 : tree.New(1)_c1 : make(chan int, 100)Walk(_t1, _c1)_t2 : tree.New(1)_c2 : make(chan int, 100)Walk(_t2, _c2)fmt.Println(_c)
}