Skip to content

Commit e10eaa9

Browse files
committed
added leetcode 297
1 parent 427b25d commit e10eaa9

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.toml
2+
.idea
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package leetcode
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type TreeNode = structures.TreeNode
11+
12+
type Codec struct {
13+
builder strings.Builder
14+
input []string
15+
}
16+
17+
func Constructor() Codec {
18+
return Codec{}
19+
}
20+
21+
// Serializes a tree to a single string.
22+
func (this *Codec) serialize(root *TreeNode) string {
23+
if root == nil {
24+
this.builder.WriteString("#,")
25+
return ""
26+
}
27+
28+
this.builder.WriteString(strconv.Itoa(root.Val)+",")
29+
this.serialize(root.Left)
30+
this.serialize(root.Right)
31+
32+
return this.builder.String()
33+
}
34+
35+
// Deserializes your encoded data to tree.
36+
func (this *Codec) deserialize(data string) *TreeNode {
37+
if len(data) == 0 {
38+
return nil
39+
}
40+
this.input = strings.Split(data, ",")
41+
return this.deserializeHelper()
42+
}
43+
44+
func (this *Codec) deserializeHelper() *TreeNode {
45+
if this.input[0] == "#" {
46+
this.input = this.input[1:]
47+
return nil
48+
}
49+
50+
val, _ := strconv.Atoi(this.input[0])
51+
this.input = this.input[1:]
52+
53+
return &TreeNode{
54+
Val: val,
55+
Left: this.deserializeHelper(),
56+
Right: this.deserializeHelper(),
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question297 struct {
11+
para297
12+
ans297
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para297 struct {
18+
one []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans297 struct {
24+
one []int
25+
}
26+
27+
func Test_Problem297(t *testing.T) {
28+
qs := []question297{
29+
{
30+
para297{[]int{}},
31+
ans297{[]int{}},
32+
},
33+
{
34+
para297{[]int{1,2,3,-1,-1,4,5}},
35+
ans297{[]int{1,2,3,-1,-1,4,5}},
36+
},
37+
{
38+
para297{[]int{1,2}},
39+
ans297{[]int{1,2}},
40+
},
41+
}
42+
43+
fmt.Printf("------------------------Leetcode Problem 297------------------------\n")
44+
45+
for _, q := range qs {
46+
_, p := q.ans297, q.para297
47+
fmt.Printf("【input】:%v ", p)
48+
root := structures.Ints2TreeNode(p.one)
49+
50+
tree297 := Constructor()
51+
serialized := tree297.serialize(root)
52+
fmt.Printf("【output】:%v \n", structures.Tree2Preorder(tree297.deserialize(serialized)))
53+
}
54+
fmt.Printf("\n\n\n")
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [297. Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)
2+
3+
## 题目
4+
5+
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.
6+
7+
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.
8+
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+
11+
Example 1:
12+
13+
```
14+
Input: root = [1,2,3,null,null,4,5]
15+
Output: [1,2,3,null,null,4,5]
16+
```
17+
18+
Example 2:
19+
20+
```
21+
Input: root = []
22+
Output: []
23+
```
24+
25+
Example 3:
26+
27+
```
28+
Input: root = [1]
29+
Output: [1]
30+
```
31+
32+
Example 4:
33+
34+
```
35+
Input: root = [1,2]
36+
Output: [1,2]
37+
```
38+
39+
Constraints:
40+
41+
* The number of nodes in the tree is in the range [0, 104].
42+
* -1000 <= Node.val <= 1000
43+
44+
## 题目大意
45+
46+
设计一个算法,来序列化和反序列化二叉树。并不限制如何进行序列化和反序列化,但是你需要保证二叉树可以序列化为字符串,并且这个字符串可以被反序列化成原有的二叉树。
47+
48+
## 解题思路
49+
50+
1. 将给定的二叉树想象成一颗满二叉树(不存在的结点用 null 填充)。
51+
2. 通过前序遍历,可以得到一个第一个结点为根的序列,然后递归进行序列化/反序列化即可。

0 commit comments

Comments
 (0)