Skip to content

Commit 417635f

Browse files
committed
Update solution 297
1 parent aa5bccb commit 417635f

File tree

13 files changed

+291
-108
lines changed

13 files changed

+291
-108
lines changed

README.md

Lines changed: 80 additions & 76 deletions
Large diffs are not rendered by default.

leetcode/0297.Serialize-and-Deserialize-Binary-Tree/297.Serialize and Deserialize Binary Tree.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type TreeNode = structures.TreeNode
1111

1212
type Codec struct {
1313
builder strings.Builder
14-
input []string
14+
input []string
1515
}
1616

1717
func Constructor() Codec {
@@ -24,11 +24,9 @@ func (this *Codec) serialize(root *TreeNode) string {
2424
this.builder.WriteString("#,")
2525
return ""
2626
}
27-
28-
this.builder.WriteString(strconv.Itoa(root.Val)+",")
27+
this.builder.WriteString(strconv.Itoa(root.Val) + ",")
2928
this.serialize(root.Left)
3029
this.serialize(root.Right)
31-
3230
return this.builder.String()
3331
}
3432

@@ -46,13 +44,11 @@ func (this *Codec) deserializeHelper() *TreeNode {
4644
this.input = this.input[1:]
4745
return nil
4846
}
49-
5047
val, _ := strconv.Atoi(this.input[0])
5148
this.input = this.input[1:]
52-
5349
return &TreeNode{
54-
Val: val,
55-
Left: this.deserializeHelper(),
50+
Val: val,
51+
Left: this.deserializeHelper(),
5652
Right: this.deserializeHelper(),
5753
}
58-
}
54+
}
Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,113 @@
11
# [297. Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)
22

3+
34
## 题目
45

56
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
67

78
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
89

9-
**Clarification:** The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
10+
**Clarification:** The input/output format is the same as [how LeetCode serializes a binary tree](https://leetcode.com/faq/#binary-tree). You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
11+
12+
**Example 1:**
1013

11-
Example 1:
14+
![https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg](https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg)
1215

1316
```
1417
Input: root = [1,2,3,null,null,4,5]
1518
Output: [1,2,3,null,null,4,5]
1619
```
1720

18-
Example 2:
21+
**Example 2:**
1922

2023
```
2124
Input: root = []
2225
Output: []
2326
```
2427

25-
Example 3:
28+
**Example 3:**
2629

2730
```
2831
Input: root = [1]
2932
Output: [1]
3033
```
3134

32-
Example 4:
35+
**Example 4:**
3336

3437
```
3538
Input: root = [1,2]
3639
Output: [1,2]
3740
```
3841

39-
Constraints:
42+
**Constraints:**
4043

41-
* The number of nodes in the tree is in the range [0, 104].
42-
* -1000 <= Node.val <= 1000
44+
- The number of nodes in the tree is in the range `[0, 104]`.
45+
- `1000 <= Node.val <= 1000`
4346

4447
## 题目大意
4548

4649
设计一个算法,来序列化和反序列化二叉树。并不限制如何进行序列化和反序列化,但是你需要保证二叉树可以序列化为字符串,并且这个字符串可以被反序列化成原有的二叉树。
4750

4851
## 解题思路
4952

50-
1. 将给定的二叉树想象成一颗满二叉树(不存在的结点用 null 填充)。
51-
2. 通过前序遍历,可以得到一个第一个结点为根的序列,然后递归进行序列化/反序列化即可。
53+
- 1. 将给定的二叉树想象成一颗满二叉树(不存在的结点用 null 填充)。
54+
- 2. 通过前序遍历,可以得到一个第一个结点为根的序列,然后递归进行序列化/反序列化即可。
55+
56+
## 代码
57+
58+
```go
59+
package leetcode
60+
61+
import (
62+
"strconv"
63+
"strings"
64+
65+
"github.com/halfrost/LeetCode-Go/structures"
66+
)
67+
68+
type TreeNode = structures.TreeNode
69+
70+
type Codec struct {
71+
builder strings.Builder
72+
input []string
73+
}
74+
75+
func Constructor() Codec {
76+
return Codec{}
77+
}
78+
79+
// Serializes a tree to a single string.
80+
func (this *Codec) serialize(root *TreeNode) string {
81+
if root == nil {
82+
this.builder.WriteString("#,")
83+
return ""
84+
}
85+
this.builder.WriteString(strconv.Itoa(root.Val) + ",")
86+
this.serialize(root.Left)
87+
this.serialize(root.Right)
88+
return this.builder.String()
89+
}
90+
91+
// Deserializes your encoded data to tree.
92+
func (this *Codec) deserialize(data string) *TreeNode {
93+
if len(data) == 0 {
94+
return nil
95+
}
96+
this.input = strings.Split(data, ",")
97+
return this.deserializeHelper()
98+
}
99+
100+
func (this *Codec) deserializeHelper() *TreeNode {
101+
if this.input[0] == "#" {
102+
this.input = this.input[1:]
103+
return nil
104+
}
105+
val, _ := strconv.Atoi(this.input[0])
106+
this.input = this.input[1:]
107+
return &TreeNode{
108+
Val: val,
109+
Left: this.deserializeHelper(),
110+
Right: this.deserializeHelper(),
111+
}
112+
}
113+
```

website/content/ChapterFour/0200~0299/0290.Word-Pattern.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ func wordPattern(pattern string, str string) bool {
101101
----------------------------------------------
102102
<div style="display: flex;justify-content: space-between;align-items: center;">
103103
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0200~0299/0287.Find-the-Duplicate-Number/">⬅️上一页</a></p>
104-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0300.Longest-Increasing-Subsequence/">下一页➡️</a></p>
104+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0200~0299/0297.Serialize-and-Deserialize-Binary-Tree/">下一页➡️</a></p>
105105
</div>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# [297. Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)
2+
3+
4+
## 题目
5+
6+
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
7+
8+
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
9+
10+
**Clarification:** The input/output format is the same as [how LeetCode serializes a binary tree](https://leetcode.com/faq/#binary-tree). You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
11+
12+
**Example 1:**
13+
14+
![https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg](https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg)
15+
16+
```
17+
Input: root = [1,2,3,null,null,4,5]
18+
Output: [1,2,3,null,null,4,5]
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: root = []
25+
Output: []
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: root = [1]
32+
Output: [1]
33+
```
34+
35+
**Example 4:**
36+
37+
```
38+
Input: root = [1,2]
39+
Output: [1,2]
40+
```
41+
42+
**Constraints:**
43+
44+
- The number of nodes in the tree is in the range `[0, 104]`.
45+
- `1000 <= Node.val <= 1000`
46+
47+
## 题目大意
48+
49+
设计一个算法,来序列化和反序列化二叉树。并不限制如何进行序列化和反序列化,但是你需要保证二叉树可以序列化为字符串,并且这个字符串可以被反序列化成原有的二叉树。
50+
51+
## 解题思路
52+
53+
- 1. 将给定的二叉树想象成一颗满二叉树(不存在的结点用 null 填充)。
54+
- 2. 通过前序遍历,可以得到一个第一个结点为根的序列,然后递归进行序列化/反序列化即可。
55+
56+
## 代码
57+
58+
```go
59+
package leetcode
60+
61+
import (
62+
"strconv"
63+
"strings"
64+
65+
"github.com/halfrost/LeetCode-Go/structures"
66+
)
67+
68+
type TreeNode = structures.TreeNode
69+
70+
type Codec struct {
71+
builder strings.Builder
72+
input []string
73+
}
74+
75+
func Constructor() Codec {
76+
return Codec{}
77+
}
78+
79+
// Serializes a tree to a single string.
80+
func (this *Codec) serialize(root *TreeNode) string {
81+
if root == nil {
82+
this.builder.WriteString("#,")
83+
return ""
84+
}
85+
this.builder.WriteString(strconv.Itoa(root.Val) + ",")
86+
this.serialize(root.Left)
87+
this.serialize(root.Right)
88+
return this.builder.String()
89+
}
90+
91+
// Deserializes your encoded data to tree.
92+
func (this *Codec) deserialize(data string) *TreeNode {
93+
if len(data) == 0 {
94+
return nil
95+
}
96+
this.input = strings.Split(data, ",")
97+
return this.deserializeHelper()
98+
}
99+
100+
func (this *Codec) deserializeHelper() *TreeNode {
101+
if this.input[0] == "#" {
102+
this.input = this.input[1:]
103+
return nil
104+
}
105+
val, _ := strconv.Atoi(this.input[0])
106+
this.input = this.input[1:]
107+
return &TreeNode{
108+
Val: val,
109+
Left: this.deserializeHelper(),
110+
Right: this.deserializeHelper(),
111+
}
112+
}
113+
```
114+
115+
116+
----------------------------------------------
117+
<div style="display: flex;justify-content: space-between;align-items: center;">
118+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0200~0299/0290.Word-Pattern/">⬅️上一页</a></p>
119+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0300.Longest-Increasing-Subsequence/">下一页➡️</a></p>
120+
</div>

website/content/ChapterFour/0300~0399/0300.Longest-Increasing-Subsequence.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ func lengthOfLIS1(nums []int) int {
8282

8383
----------------------------------------------
8484
<div style="display: flex;justify-content: space-between;align-items: center;">
85-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0200~0299/0290.Word-Pattern/">⬅️上一页</a></p>
85+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0200~0299/0297.Serialize-and-Deserialize-Binary-Tree/">⬅️上一页</a></p>
8686
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0303.Range-Sum-Query-Immutable/">下一页➡️</a></p>
8787
</div>

website/content/ChapterTwo/Array.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ weight: 1
6767
|0217|Contains Duplicate|[Go]({{< relref "/ChapterFour/0200~0299/0217.Contains-Duplicate.md" >}})|Easy| O(n)| O(n)||56.8%|
6868
|0219|Contains Duplicate II|[Go]({{< relref "/ChapterFour/0200~0299/0219.Contains-Duplicate-II.md" >}})|Easy| O(n)| O(n)||38.8%|
6969
|0228|Summary Ranges|[Go]({{< relref "/ChapterFour/0200~0299/0228.Summary-Ranges.md" >}})|Easy||||42.6%|
70-
|0229|Majority Element II|[Go]({{< relref "/ChapterFour/0200~0299/0229.Majority-Element-II.md" >}})|Medium||||38.9%|
70+
|0229|Majority Element II|[Go]({{< relref "/ChapterFour/0200~0299/0229.Majority-Element-II.md" >}})|Medium||||39.0%|
7171
|0268|Missing Number|[Go]({{< relref "/ChapterFour/0200~0299/0268.Missing-Number.md" >}})|Easy||||55.1%|
7272
|0283|Move Zeroes|[Go]({{< relref "/ChapterFour/0200~0299/0283.Move-Zeroes.md" >}})|Easy| O(n)| O(1)||58.6%|
7373
|0287|Find the Duplicate Number|[Go]({{< relref "/ChapterFour/0200~0299/0287.Find-the-Duplicate-Number.md" >}})|Medium| O(n)| O(1)|❤️|57.8%|
@@ -92,7 +92,7 @@ weight: 1
9292
|0714|Best Time to Buy and Sell Stock with Transaction Fee|[Go]({{< relref "/ChapterFour/0700~0799/0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee.md" >}})|Medium| O(n)| O(1)||58.0%|
9393
|0717|1-bit and 2-bit Characters|[Go]({{< relref "/ChapterFour/0700~0799/0717.1-bit-and-2-bit-Characters.md" >}})|Easy||||47.0%|
9494
|0718|Maximum Length of Repeated Subarray|[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})|Medium||||50.4%|
95-
|0719|Find K-th Smallest Pair Distance|[Go]({{< relref "/ChapterFour/0700~0799/0719.Find-K-th-Smallest-Pair-Distance.md" >}})|Hard||||32.6%|
95+
|0719|Find K-th Smallest Pair Distance|[Go]({{< relref "/ChapterFour/0700~0799/0719.Find-K-th-Smallest-Pair-Distance.md" >}})|Hard||||32.7%|
9696
|0724|Find Pivot Index|[Go]({{< relref "/ChapterFour/0700~0799/0724.Find-Pivot-Index.md" >}})|Easy||||45.9%|
9797
|0729|My Calendar I|[Go]({{< relref "/ChapterFour/0700~0799/0729.My-Calendar-I.md" >}})|Medium||||53.5%|
9898
|0746|Min Cost Climbing Stairs|[Go]({{< relref "/ChapterFour/0700~0799/0746.Min-Cost-Climbing-Stairs.md" >}})|Easy| O(n)| O(1)||51.0%|
@@ -134,7 +134,7 @@ weight: 1
134134
|1208|Get Equal Substrings Within Budget|[Go]({{< relref "/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget.md" >}})|Medium||||44.2%|
135135
|1217|Minimum Cost to Move Chips to The Same Position|[Go]({{< relref "/ChapterFour/1200~1299/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position.md" >}})|Easy||||71.1%|
136136
|1232|Check If It Is a Straight Line|[Go]({{< relref "/ChapterFour/1200~1299/1232.Check-If-It-Is-a-Straight-Line.md" >}})|Easy||||43.3%|
137-
|1252|Cells with Odd Values in a Matrix|[Go]({{< relref "/ChapterFour/1200~1299/1252.Cells-with-Odd-Values-in-a-Matrix.md" >}})|Easy||||78.8%|
137+
|1252|Cells with Odd Values in a Matrix|[Go]({{< relref "/ChapterFour/1200~1299/1252.Cells-with-Odd-Values-in-a-Matrix.md" >}})|Easy||||78.7%|
138138
|1260|Shift 2D Grid|[Go]({{< relref "/ChapterFour/1200~1299/1260.Shift-2D-Grid.md" >}})|Easy||||61.8%|
139139
|1266|Minimum Time Visiting All Points|[Go]({{< relref "/ChapterFour/1200~1299/1266.Minimum-Time-Visiting-All-Points.md" >}})|Easy||||79.3%|
140140
|1275|Find Winner on a Tic Tac Toe Game|[Go]({{< relref "/ChapterFour/1200~1299/1275.Find-Winner-on-a-Tic-Tac-Toe-Game.md" >}})|Easy||||52.8%|

website/content/ChapterTwo/Binary_Indexed_Tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ weight: 19
1111
| No. | Title | Solution | Difficulty | TimeComplexity | SpaceComplexity |Favorite| Acceptance |
1212
|:--------:|:------- | :--------: | :----------: | :----: | :-----: | :-----: |:-----: |
1313
|0218|The Skyline Problem|[Go]({{< relref "/ChapterFour/0200~0299/0218.The-Skyline-Problem.md" >}})|Hard||||36.5%|
14-
|0307|Range Sum Query - Mutable|[Go]({{< relref "/ChapterFour/0300~0399/0307.Range-Sum-Query-Mutable.md" >}})|Medium||||36.8%|
14+
|0307|Range Sum Query - Mutable|[Go]({{< relref "/ChapterFour/0300~0399/0307.Range-Sum-Query-Mutable.md" >}})|Medium||||36.9%|
1515
|0315|Count of Smaller Numbers After Self|[Go]({{< relref "/ChapterFour/0300~0399/0315.Count-of-Smaller-Numbers-After-Self.md" >}})|Hard||||42.4%|
1616
|0327|Count of Range Sum|[Go]({{< relref "/ChapterFour/0300~0399/0327.Count-of-Range-Sum.md" >}})|Hard||||36.1%|
1717
|0493|Reverse Pairs|[Go]({{< relref "/ChapterFour/0400~0499/0493.Reverse-Pairs.md" >}})|Hard||||27.0%|

website/content/ChapterTwo/Binary_Search.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ func peakIndexInMountainArray(A []int) int {
174174
|0704|Binary Search|[Go]({{< relref "/ChapterFour/0700~0799/0704.Binary-Search.md" >}})|Easy||||54.2%|
175175
|0710|Random Pick with Blacklist|[Go]({{< relref "/ChapterFour/0700~0799/0710.Random-Pick-with-Blacklist.md" >}})|Hard| O(n)| O(n) ||33.0%|
176176
|0718|Maximum Length of Repeated Subarray|[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})|Medium||||50.4%|
177-
|0719|Find K-th Smallest Pair Distance|[Go]({{< relref "/ChapterFour/0700~0799/0719.Find-K-th-Smallest-Pair-Distance.md" >}})|Hard||||32.6%|
177+
|0719|Find K-th Smallest Pair Distance|[Go]({{< relref "/ChapterFour/0700~0799/0719.Find-K-th-Smallest-Pair-Distance.md" >}})|Hard||||32.7%|
178178
|0744|Find Smallest Letter Greater Than Target|[Go]({{< relref "/ChapterFour/0700~0799/0744.Find-Smallest-Letter-Greater-Than-Target.md" >}})|Easy||||45.5%|
179179
|0778|Swim in Rising Water|[Go]({{< relref "/ChapterFour/0700~0799/0778.Swim-in-Rising-Water.md" >}})|Hard||||54.8%|
180180
|0786|K-th Smallest Prime Fraction|[Go]({{< relref "/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction.md" >}})|Hard||||43.1%|
181181
|0793|Preimage Size of Factorial Zeroes Function|[Go]({{< relref "/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function.md" >}})|Hard||||40.6%|
182182
|0852|Peak Index in a Mountain Array|[Go]({{< relref "/ChapterFour/0800~0899/0852.Peak-Index-in-a-Mountain-Array.md" >}})|Easy||||71.7%|
183-
|0862|Shortest Subarray with Sum at Least K|[Go]({{< relref "/ChapterFour/0800~0899/0862.Shortest-Subarray-with-Sum-at-Least-K.md" >}})|Hard||||25.3%|
183+
|0862|Shortest Subarray with Sum at Least K|[Go]({{< relref "/ChapterFour/0800~0899/0862.Shortest-Subarray-with-Sum-at-Least-K.md" >}})|Hard||||25.2%|
184184
|0875|Koko Eating Bananas|[Go]({{< relref "/ChapterFour/0800~0899/0875.Koko-Eating-Bananas.md" >}})|Medium||||53.5%|
185185
|0878|Nth Magical Number|[Go]({{< relref "/ChapterFour/0800~0899/0878.Nth-Magical-Number.md" >}})|Hard||||28.9%|
186186
|0887|Super Egg Drop|[Go]({{< relref "/ChapterFour/0800~0899/0887.Super-Egg-Drop.md" >}})|Hard||||27.0%|

website/content/ChapterTwo/Segment_Tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ weight: 18
3838
| No. | Title | Solution | Difficulty | TimeComplexity | SpaceComplexity |Favorite| Acceptance |
3939
|:--------:|:------- | :--------: | :----------: | :----: | :-----: | :-----: |:-----: |
4040
|0218|The Skyline Problem|[Go]({{< relref "/ChapterFour/0200~0299/0218.The-Skyline-Problem.md" >}})|Hard| O(n log n)| O(n)|❤️|36.5%|
41-
|0307|Range Sum Query - Mutable|[Go]({{< relref "/ChapterFour/0300~0399/0307.Range-Sum-Query-Mutable.md" >}})|Medium| O(1)| O(n)||36.8%|
41+
|0307|Range Sum Query - Mutable|[Go]({{< relref "/ChapterFour/0300~0399/0307.Range-Sum-Query-Mutable.md" >}})|Medium| O(1)| O(n)||36.9%|
4242
|0315|Count of Smaller Numbers After Self|[Go]({{< relref "/ChapterFour/0300~0399/0315.Count-of-Smaller-Numbers-After-Self.md" >}})|Hard| O(n log n)| O(n)||42.4%|
4343
|0327|Count of Range Sum|[Go]({{< relref "/ChapterFour/0300~0399/0327.Count-of-Range-Sum.md" >}})|Hard| O(n log n)| O(n)|❤️|36.1%|
4444
|0493|Reverse Pairs|[Go]({{< relref "/ChapterFour/0400~0499/0493.Reverse-Pairs.md" >}})|Hard| O(n log n)| O(n)||27.0%|

0 commit comments

Comments
 (0)