-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path101.go
48 lines (44 loc) · 933 Bytes
/
101.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 easy_collection
/**
* 101. Symmetric Tree
* <p>
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
* <p>
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
* <p>
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
* <p>
* But the following [1,2,2,null,3,null,3] is not:
* <p>
* 1
* / \
* 2 2
* \ \
* 3 3
* <p>
* Note:
* <p>
* Bonus points if you could solve it both recursively and iteratively.
*/
// TreeNode Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func isSymmetric(root *TreeNode) bool {
return root == nil || helper101(root.Left, root.Right)
}
func helper101(left, right *TreeNode) bool {
if left == nil || right == nil {
return left == right
}
if left.Val != right.Val {
return false
}
return helper101(left.Left, right.Right) && helper101(left.Right, right.Left)
}