package leetcode import ( "strconv" "strings" "github.com/halfrost/LeetCode-Go/structures" ) type TreeNode = structures.TreeNode type Codec struct { builder strings.Builder input []string } func Constructor() Codec { return Codec{} } // Serializes a tree to a single string. func (this *Codec) serialize(root *TreeNode) string { if root == nil { this.builder.WriteString("#,") return "" } this.builder.WriteString(strconv.Itoa(root.Val) + ",") this.serialize(root.Left) this.serialize(root.Right) return this.builder.String() } // Deserializes your encoded data to tree. func (this *Codec) deserialize(data string) *TreeNode { if len(data) == 0 { return nil } this.input = strings.Split(data, ",") return this.deserializeHelper() } func (this *Codec) deserializeHelper() *TreeNode { if this.input[0] == "#" { this.input = this.input[1:] return nil } val, _ := strconv.Atoi(this.input[0]) this.input = this.input[1:] return &TreeNode{ Val: val, Left: this.deserializeHelper(), Right: this.deserializeHelper(), } }