Created
December 31, 2019 22:53
-
-
Save delongGao/4e5d9b4070ef5d0018938a365a487af8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
// DFS in order | |
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, 10), make(chan int, 10) | |
go Walk(t1, ch1) | |
go Walk(t2, ch2) | |
for i := 0; i < cap(ch1); i++ { | |
if <-ch1 != <-ch2 { | |
return false | |
} | |
} | |
return true | |
} | |
func main() { | |
// ch := make(chan int, 10) | |
// go Walk(tree.New(1), ch) | |
// for i := 0; i < cap(ch); i++ { | |
// fmt.Println(<-ch) | |
// } | |
// just test the string representation of a tree | |
fmt.Println(tree.New(1).String()) | |
res_t := Same(tree.New(1), tree.New(1)) | |
res_f := Same(tree.New(1), tree.New(2)) | |
fmt.Println(res_t) | |
fmt.Println(res_f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment