Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "golang.org/x/tour/tree"
- import "fmt"
- func Walk(t *tree.Tree, ch chan int) {
- walkRecursive(t, ch)
- close(ch)
- }
- func walkRecursive(t *tree.Tree, ch chan int) {
- ch <- t.Value
- if t.Left != nil {
- walkRecursive(t.Left, ch)
- }
- if t.Right != nil {
- walkRecursive(t.Right, ch)
- }
- }
- func Same(t1, t2 *tree.Tree) bool {
- ch1 := make(chan int)
- go Walk(t1, ch1)
- for checkVal := range ch1 {
- found := false
- ch2 := make(chan int)
- go Walk(t2, ch2)
- for checkVal2 := range ch2 {
- if checkVal == checkVal2 {
- found = true
- break
- }
- }
- if !found {
- return false
- }
- }
- return true
- }
- func main() {
- t1 := tree.New(2)
- t2 := tree.New(1)
- res := Same(t1, t2)
- fmt.Printf("Result: %v \n", res)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement