Skip to content

Commit 5779f04

Browse files
committed
feat(solutions): add symmetric_tree
1 parent 05de761 commit 5779f04

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ If you like it then you can put a :star:**Star** on it.
3434
3535
| # | **Problem** & **Solution** | Difficulty | Single Repetition Duration | LeetCode Run Time |
3636
| ---: | :----- | :----------: | ----------: | ----------: |
37+
|[101][Solutions-101]|[Symmetric Tree][Solutions-101-Home]|Easy|[17.6 ns/op][Solutions-101-Code] / [5 test cases][Solutions-101-Test]|4 ms|
3738
|[100][Solutions-100]|[Same Tree][Solutions-100-Home]|Easy|[9.12 ns/op][Solutions-100-Code] / [6 test cases][Solutions-100-Test]|0 ms|
3839
|[99][Solutions-99]|[Recover Binary Search Tree][Solutions-99-Home]|Hard|[94.8 ns/op][Solutions-99-Code] / [3 test cases][Solutions-99-Test]|32 ms|
3940
|[98][Solutions-98]|[Validate Binary Search Tree][Solutions-98-Home]|Medium|[21.0 ns/op][Solutions-98-Code] / [8 test cases][Solutions-98-Test]|8 ms|
@@ -156,6 +157,10 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
156157
## License
157158
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FWindomZ%2Fleetcode.go.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FWindomZ%2Fleetcode.go?ref=badge_large)
158159

160+
[Solutions-101]:https://leetcode.com/problems/symmetric-tree/
161+
[Solutions-101-Home]:solutions/symmetric_tree/
162+
[Solutions-101-Code]:solutions/symmetric_tree/symmetrictree.go
163+
[Solutions-101-Test]:solutions/symmetric_tree/symmetrictree_test.go#L52
159164
[Solutions-100]:https://leetcode.com/problems/same-tree/
160165
[Solutions-100-Home]:solutions/same_tree/
161166
[Solutions-100-Code]:solutions/same_tree/sametree.go

Diff for: solutions/symmetric_tree/NOTE_Ch-zh.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 解法要点
2+
1. 不验证根节点的镜像
3+
1. 通过递归遍历方式,验证`左右`叶节和`右左`叶节点是否相等

Diff for: solutions/symmetric_tree/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [101. Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/)
2+
3+
## Description
4+
5+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
6+
7+
For example, this binary tree `[1,2,2,3,4,4,3]` is symmetric:
8+
```
9+
1
10+
/ \
11+
2 2
12+
/ \ / \
13+
3 4 4 3
14+
```
15+
16+
But the following `[1,2,2,null,3,null,3]` is not:
17+
18+
```
19+
1
20+
/ \
21+
2 2
22+
\ \
23+
3 3
24+
```
25+
26+
**Note:**
27+
28+
Bonus points if you could solve it both recursively and iteratively.
29+
30+
## Solution
31+
- [Code](symmetrictree.go)
32+
- [Testing](symmetrictree_test.go)
33+
34+
## Note
35+
- [中文](NOTE_Ch-zh.md)

Diff for: solutions/symmetric_tree/symmetrictree.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package symmetrictree
2+
3+
// TreeNode definition for a binary tree node.
4+
type TreeNode struct {
5+
Val int
6+
Left *TreeNode
7+
Right *TreeNode
8+
}
9+
10+
func isSymmetric(root *TreeNode) bool {
11+
return isMirror(root, root)
12+
}
13+
14+
func isMirror(t1, t2 *TreeNode) bool {
15+
if t1 == nil && t2 == nil {
16+
return true
17+
}
18+
if t1 == nil || t2 == nil {
19+
return false
20+
}
21+
return t1.Val == t2.Val && isMirror(t1.Right, t2.Left) && isMirror(t1.Left, t2.Right)
22+
}

Diff for: solutions/symmetric_tree/symmetrictree_test.go

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package symmetrictree
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_isSymmetric(t *testing.T) {
10+
assert.True(t, isSymmetric(nil))
11+
assert.True(t, isSymmetric(&TreeNode{Val: 0}))
12+
assert.True(t, isSymmetric(&TreeNode{
13+
Val: 0,
14+
Left: &TreeNode{
15+
Val: 1,
16+
Left: &TreeNode{Val: 1},
17+
Right: &TreeNode{Val: 3},
18+
},
19+
Right: &TreeNode{
20+
Val: 1,
21+
Left: &TreeNode{Val: 3},
22+
Right: &TreeNode{Val: 1},
23+
},
24+
}))
25+
assert.False(t, isSymmetric(&TreeNode{
26+
Val: 0,
27+
Left: &TreeNode{
28+
Val: 1,
29+
Left: &TreeNode{Val: 1},
30+
Right: &TreeNode{Val: 3},
31+
},
32+
Right: &TreeNode{
33+
Val: 1,
34+
Right: &TreeNode{Val: 1},
35+
},
36+
}))
37+
assert.False(t, isSymmetric(&TreeNode{
38+
Val: 0,
39+
Left: &TreeNode{
40+
Val: 1,
41+
Left: &TreeNode{Val: 1},
42+
Right: &TreeNode{Val: 3},
43+
},
44+
Right: &TreeNode{
45+
Val: 1,
46+
Left: &TreeNode{Val: 2},
47+
Right: &TreeNode{Val: 1},
48+
},
49+
}))
50+
}
51+
52+
func Benchmark_isSymmetric(b *testing.B) {
53+
b.StopTimer()
54+
b.ReportAllocs()
55+
b.StartTimer()
56+
b.RunParallel(func(pb *testing.PB) {
57+
for pb.Next() {
58+
isSymmetric(nil)
59+
isSymmetric(&TreeNode{Val: 0})
60+
isSymmetric(&TreeNode{
61+
Val: 0,
62+
Left: &TreeNode{
63+
Val: 1,
64+
Left: &TreeNode{Val: 1},
65+
Right: &TreeNode{Val: 3},
66+
},
67+
Right: &TreeNode{
68+
Val: 1,
69+
Left: &TreeNode{Val: 3},
70+
Right: &TreeNode{Val: 1},
71+
},
72+
})
73+
isSymmetric(&TreeNode{
74+
Val: 0,
75+
Left: &TreeNode{
76+
Val: 1,
77+
Left: &TreeNode{Val: 1},
78+
Right: &TreeNode{Val: 3},
79+
},
80+
Right: &TreeNode{
81+
Val: 1,
82+
Right: &TreeNode{Val: 1},
83+
},
84+
})
85+
isSymmetric(&TreeNode{
86+
Val: 0,
87+
Left: &TreeNode{
88+
Val: 1,
89+
Left: &TreeNode{Val: 1},
90+
Right: &TreeNode{Val: 3},
91+
},
92+
Right: &TreeNode{
93+
Val: 1,
94+
Left: &TreeNode{Val: 2},
95+
Right: &TreeNode{Val: 1},
96+
},
97+
})
98+
}
99+
})
100+
}

0 commit comments

Comments
 (0)