当前位置:网站首页>2022-01-25: serialize and deserialize n-ary tree. Serialization means that a
2022-01-25: serialize and deserialize n-ary tree. Serialization means that a
2022-06-23 02:59:00 【Fuda scaffold constructor's daily question】
2022-01-25: Serialization and deserialization N Fork tree .
Serialization is the process of converting a data structure into a sequence of bits , So you can store it in a file or in a memory buffer , So that the structure can be restored later in the same or different computer environment .
Design a serialization and deserialization N Fork tree algorithm .
One N A fork tree means that each node has no more than N A rooted tree with child nodes .
serialize / There are no restrictions on the algorithm implementation of deserialization algorithm .
You just need to guarantee N The fork tree can be serialized into a string and the string can be deserialized into the original tree structure .
Be careful :
N The scope of 1, 1000
Don't use class members / Global variables / Static variables to store state .
Your serialization and deserialization algorithms should be stateless .
Power button 428.
answer 2022-01-25:
Natural intelligence . recursive .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
a := NewNode2(1)
b := NewNode2(2)
c := NewNode2(3)
d := NewNode2(4)
e := NewNode2(5)
f := NewNode2(6)
g := NewNode2(7)
a.children = append(a.children, b)
a.children = append(a.children, c)
a.children = append(a.children, d)
b.children = append(b.children, e)
b.children = append(b.children, f)
e.children = append(e.children, g)
codec := &Codec{}
content := codec.serialize(a)
fmt.Println(content)
head := codec.deserialize(content)
fmt.Println(codec.serialize(head) == content)
}
// Don't submit this class
type Node struct {
val int
children []*Node
}
func NewNode1() *Node {
ret := &Node{}
ret.children = make([]*Node, 0)
return ret
}
func NewNode2(_val int) *Node {
ret := &Node{}
ret.val = _val
ret.children = make([]*Node, 0)
return ret
}
func NewNode3(_val int, _children []*Node) *Node {
ret := &Node{}
ret.val = _val
ret.children = _children
return ret
}
// Submit the following class
type Codec struct {
}
func (this *Codec) serialize(root *Node) string {
if root == nil { // Empty tree ! Go straight back to #
return "#"
}
builder := ""
this.serial(&builder, root)
return builder
}
// Now comes head
func (this *Codec) serial(builder *string, head *Node) {
*builder += fmt.Sprint(head.val) + ","
if len(head.children) > 0 {
*builder += "[,"
for _, next := range head.children {
this.serial(builder, next)
}
*builder += "],"
}
}
func (this *Codec) deserialize(data string) *Node {
if data == "#" {
return nil
}
splits := strings.Split(data, ",")
queue := make([]string, 0)
for _, str := range splits {
queue = append(queue, str)
}
return this.deserial(&queue)
}
func (this *Codec) deserial(queue *[]string) *Node {
v, _ := strconv.Atoi((*queue)[0])
cur := NewNode2(v)
*queue = (*queue)[1:]
cur.children = make([]*Node, 0)
if len(*queue) > 0 && (*queue)[0] == "[" {
*queue = (*queue)[1:]
for (*queue)[0] != "]" {
child := this.deserial(queue)
cur.children = append(cur.children, child)
}
*queue = (*queue)[1:]
}
return cur
}The results are as follows :
边栏推荐
- Automatically update site statistics with actions
- Reading redis source code (V) master-slave replication and sentinel mechanism
- JS event delegation (event agent)
- What is a smart farm?
- Markdown - mark above / below symbol (typora, latex)
- Tencent cloud server CVM system tool configuration
- Call rest port to implement nailing notification
- February 1, 2022: painting house II. If there is a row of houses, N in total, each
- "Return index" of live broadcast E-commerce
- Hypervisor Necromancy; Recover kernel protector (2)
猜你喜欢

5. concept of ruler method

C language series - Section 4 - arrays
What is sitelock? What is the function?

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

Spark broadcast variables and accumulators (cases attached)

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

8. greed

How to store, manage and view family photos in an orderly manner?

6. template for integer and real number dichotomy

Vulnhub DC-5
随机推荐
Qingdao stadium has made headlines again, but it has nothing to do with sports
Analysis of resolv Conf common parameters
Troubleshooting and solution of 4K video cannot be played on easydss live video on demand platform
Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
6. template for integer and real number dichotomy
Use of apicloud AVM framework list component list view and flex layout tutorial
How does native JS get the child elements of the parent element that the current element belongs to
Automatically update site statistics with actions
Analysis of ThreadLocal
C language series - Section 4 - arrays
Reading redis source code (V) master-slave replication and sentinel mechanism
DNS Service Setup
Markdown - mark above / below symbol (typora, latex)
How to use pictures in Excel in PPT template
Autowired usage
Canvas draw the clock
Detailed explanation of various networking modes of video monitoring platform
JS event bubble and event capture
February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
Understand one article: build an activity analysis system