-
Notifications
You must be signed in to change notification settings - Fork 0
/
101.symmetric-tree.go
99 lines (93 loc) · 1.91 KB
/
101.symmetric-tree.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* @lc app=leetcode id=101 lang=golang
*
* [101] Symmetric Tree
*
* https://leetcode.com/problems/symmetric-tree/description/
*
* algorithms
* Easy (46.45%)
* Likes: 4213
* Dislikes: 104
* Total Accepted: 662.9K
* Total Submissions: 1.4M
* Testcase Example: '[1,2,2,3,4,4,3]'
*
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric
* around its center).
*
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
*
*
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
*
*
*
*
* But the following [1,2,2,null,3,null,3] is not:
*
*
* 1
* / \
* 2 2
* \ \
* 3 3
*
*
*
*
* Follow up: Solve it both recursively and iteratively.
*
*/
// @lc code=start
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSymmetric(root *TreeNode) bool {
return isSymmetric1(root)
}
func isSymmetric2(root *TreeNode) bool {
stack := []*TreeNode{root, root}
for len(stack) > 0 {
node1, node2 := stack[len(stack)-1], stack[len(stack)-2]
if node1 == nil && node2 == nil {
stack = stack[:len(stack)-2]
} else {
if node1 == nil || node2 == nil {
return false
}
if node1.Val != node2.Val {
return false
}
stack = stack[:len(stack)-2]
stack = append(stack, node1.Left)
stack = append(stack, node2.Right)
stack = append(stack, node1.Right)
stack = append(stack, node2.Left)
}
}
return true
}
// solution by recursion, time complexity: O(n), space complexity: O(n)
func isSymmetric1(root *TreeNode) bool {
return isMirror(root, root)
}
func isMirror(node1 *TreeNode, node2 *TreeNode) bool {
if node1 == nil && node2 == nil {
return true
}
if node1 == nil || node2 == nil {
return false
}
return node1.Val == node2.Val && isMirror(node1.Left, node2.Right) && isMirror(node1.Right, node2.Left)
}
// @lc code=end