-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.go
48 lines (44 loc) · 876 Bytes
/
solution.go
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
package main
import "fmt"
// TreeNode is a binary tree node
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil && q == nil {
return true
} else if p == nil || q == nil {
return false
} else if p.Val != q.Val {
return false
} else {
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
}
}
func main() {
t1 := &TreeNode{1,
&TreeNode{2,
&TreeNode{4,
&TreeNode{7, nil, nil},
nil},
&TreeNode{5, nil, nil}},
&TreeNode{3,
&TreeNode{6,
&TreeNode{8, nil, nil},
&TreeNode{9, nil, nil}},
nil}}
t2 := &TreeNode{1,
&TreeNode{2,
&TreeNode{4,
&TreeNode{7, nil, nil},
nil},
&TreeNode{5, nil, nil}},
&TreeNode{3,
&TreeNode{6,
&TreeNode{8, nil, nil},
&TreeNode{9, nil, nil}},
nil}}
fmt.Println(isSameTree(t1, t2))
}