-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0101.SymmetricTree.go
More file actions
71 lines (66 loc) · 1.68 KB
/
0101.SymmetricTree.go
File metadata and controls
71 lines (66 loc) · 1.68 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
func isSymmetricIterative(root *TreeNode) bool {
if root == nil {
return true
}
if root.Left == nil && root.Right == nil {
return true
}
if root.Left == nil || root.Right == nil{
return false
}
if root.Left.Val != root.Right.Val {
return false
}
var q []*TreeNode
q = append(q, root.Left)
q = append(q, root.Right)
for len(q) > 0 {
//add next level to array
length := len(q)
level := []int{}
for i := 0; i < length; i++{
node := q[0]
var nodeVal int
if node != nil{
nodeVal = node.Val
q = append(q, node.Left)
q = append(q, node.Right)
}
level = append(level, nodeVal)
q = q[1:]
}
left, right := 0, len(level) - 1
for left < right {
if level[left] != level[right] {
return false
}
left++
right--
}
}
return true
}
func isSymmetricRecursively(root *TreeNode) bool {
if root == nil {
return true
}
return checkSymmetric(root.Left, root.Right)
}
func checkSymmetric(left, right *TreeNode) bool{
if left == nil && right == nil {
return true
}
if (left == nil || right == nil) || (left.Val != right.Val) {
return false
}
return checkSymmetric(left.Left, right.Right) && checkSymmetric(left.Right,right.Left)
}
// [1,2,2,3,4,4,3]
// [1,2,2,3,4,4,3]
// [1,2,2,null,3,null,3]
// [1,2,2,null,3,3,null]
// [1]
// [1,2,2,3,4,4,3,5,6,7,8,8,7,6,5]
// [1,2,2,3,4,4,3,5,6,7,8,8,7,7,5]
// [1,2,2,3,4,4,3,5,6,7,8,8,7,6,5,9]