101. 对称二叉树

难度: 简单




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
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSymmetric(root *TreeNode) bool {

if root == nil {
return true
}

return isSameNode(root.Left,root.Right)
}


func isSameNode(left *TreeNode,right *TreeNode) bool {

if left == nil && right == nil {
return true
}

if left == nil || right == nil {
return false
}


if left.Val != right.Val {
return false
}

return isSameNode(left.Left,right.Right) && isSameNode(left.Right,right.Left)

}