1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package main
import (
"fmt"
"sort"
"golang.org/x/tour/tree"
)
func walk(t *tree.Tree, ch chan int) {
if t == nil {
return
}
walk(t.Left, ch)
ch <- t.Value
walk(t.Right, ch)
}
// Walk 遍历 tree t 将所有的值从 tree 发送到 channel ch。
func Walk(t *tree.Tree, ch chan int) {
walk(t, ch)
close(ch)
}
// Same 检测树 t1 和 t2 是否含有相同的值。
func Same(t1, t2 *tree.Tree) bool {
ch1 := make(chan int)
ch2 := make(chan int)
a1 := make([]int, 0)
a2 := make([]int, 0)
go Walk(t1, ch1)
go Walk(t2, ch2)
for v := range ch1 {
a1 = append(a1, v)
}
for v := range ch2 {
a2 = append(a2, v)
}
if len(a1) != len(a2) {
return false
}
// 是否应当Sort?
// 题目要求的是:检测 t1 和 t2 是否存储了相同的值
sort.Ints(a1)
sort.Ints(a2)
for i, v := range a1 {
if v != a2[i] {
return false
}
}
return true
}
func main() {
// Test Walk
ch := make(chan int)
t := tree.New(1)
fmt.Println("tree:", t)
go Walk(t, ch)
for v := range ch {
fmt.Println("Got value:", v)
}
// Test Same
t2 := tree.New(1)
fmt.Println("Check same:", Same(t, t2))
}
|