MCPcopy Create free account
hub / github.com/VanjaRo/LeetCode / serialize

Method serialize

tasks/449.go:25–40  ·  view source on GitHub ↗

Serializes a tree to a single string.

(root *TreeNode)

Source from the content-addressed store, hash-verified

23
24// Serializes a tree to a single string.
25func (this *Codec) serialize(root *TreeNode) string {
26 var ret []string
27 var dfs func(head *TreeNode)
28 dfs = func(head *TreeNode) {
29 if head == nil {
30 ret = append(ret, "x")
31 return
32 }
33 ret = append(ret, strconv.Itoa(head.Val))
34 dfs(head.Left)
35 dfs(head.Right)
36 }
37 dfs(root)
38
39 return strings.Join(ret, ",")
40}
41
42// Deserializes your encoded data to tree.
43func (this *Codec) deserialize(data string) *TreeNode {

Callers

nothing calls this directly

Calls 1

dfsFunction · 0.70

Tested by

no test coverage detected