-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.go
54 lines (44 loc) · 967 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
49
50
51
52
53
54
package Solution
// 递归遍历
// 时间复杂度 O(N)
// 时间复杂度 O(tree height)
func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil && q == nil {
return true
} else if p == nil || q == nil {
return false
}
if p.Val == q.Val {
return isSameTree(p.Left, q.Left) &&
isSameTree(p.Right, q.Right)
}
return false
}
// 递归遍历
// 时间复杂度 O(N)
// 时间复杂度 O(tree height)
func isSameTree2(p *TreeNode, q *TreeNode) bool {
qP := []*TreeNode{p}
qQ := []*TreeNode{q}
for len(qP) != 0 && len(qQ) != 0 {
pNode := qP[0]
qP = qP[1:]
qNode := qQ[0]
qQ = qQ[1:]
if pNode == nil && qNode == nil {
continue
}
if pNode == nil && qNode != nil || pNode != nil && qNode == nil {
return false
}
if pNode.Val != qNode.Val {
return false
}
qP = append(qP, pNode.Left, pNode.Right)
qQ = append(qQ, qNode.Left, qNode.Right)
}
if len(qP) == 0 && len(qQ) == 0 {
return true
}
return false
}