forked from yijizhichang/LeetCodeInGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101.go
More file actions
43 lines (36 loc) · 671 Bytes
/
101.go
File metadata and controls
43 lines (36 loc) · 671 Bytes
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
package problem
/*
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
*/
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isMirror(r1, r2 *TreeNode) bool {
if r1 == nil && r2 == nil {
return true
}
if r1 == nil || r2 == nil {
return false
}
if r1.Val != r2.Val {
return false
}
return isMirror(r1.Left, r2.Right) && isMirror(r1.Right, r2.Left)
}
func isSymmetric(root *TreeNode) bool {
return isMirror(root, root)
}