|
| 1 | +// 297. Serialize and Deserialize Binary Tree |
| 2 | +// Topics: 'String', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Design', 'Binary Tree' |
| 3 | +// Level: 'Medium' |
| 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 | +// Input: root = [1,2,3,null,null,4,5] |
| 14 | +// Output: [1,2,3,null,null,4,5] |
| 15 | + |
| 16 | +// Example 2: |
| 17 | + |
| 18 | +// Input: root = [] |
| 19 | +// Output: [] |
| 20 | + |
| 21 | +// Constraints: |
| 22 | + |
| 23 | +// The number of nodes in the tree is in the range [0, 104]. |
| 24 | +// -1000 <= Node.val <= 1000 |
| 25 | + |
| 26 | +package serialiseanddesirialisebinarytree |
| 27 | + |
| 28 | +import ( |
| 29 | + "strconv" |
| 30 | + "strings" |
| 31 | +) |
| 32 | + |
| 33 | +type Codec struct { |
| 34 | +} |
| 35 | + |
| 36 | +func Constructor() Codec { |
| 37 | + return Codec{} |
| 38 | +} |
| 39 | + |
| 40 | +func (c *Codec) serialize(root *TreeNode) string { |
| 41 | + var b strings.Builder |
| 42 | + q := []*TreeNode{} |
| 43 | + q = append(q, root) |
| 44 | + |
| 45 | + for len(q) != 0 { |
| 46 | + node := q[0] |
| 47 | + q = q[1:] |
| 48 | + if b.Len() > 0 { |
| 49 | + b.WriteString(",") |
| 50 | + } |
| 51 | + if node != nil { |
| 52 | + b.WriteString(strconv.Itoa(node.Val)) |
| 53 | + q = append(q, node.Left) |
| 54 | + q = append(q, node.Right) |
| 55 | + } else { |
| 56 | + b.WriteString("nil") |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + return b.String() |
| 61 | +} |
| 62 | + |
| 63 | +func (c *Codec) deserialize(data string) *TreeNode { |
| 64 | + var root *TreeNode |
| 65 | + stream, ok := newStream(data) |
| 66 | + if !ok { |
| 67 | + return root |
| 68 | + } |
| 69 | + root = stream.next() |
| 70 | + if stream.empty() { |
| 71 | + return root |
| 72 | + } |
| 73 | + q := []*TreeNode{root} |
| 74 | + |
| 75 | + for len(q) != 0 { |
| 76 | + if stream.empty() { |
| 77 | + return root |
| 78 | + } |
| 79 | + node := q[0] |
| 80 | + q = q[1:] |
| 81 | + node.Left = stream.next() |
| 82 | + if node.Left != nil { |
| 83 | + q = append(q, node.Left) |
| 84 | + } |
| 85 | + if stream.empty() { |
| 86 | + return root |
| 87 | + } |
| 88 | + node.Right = stream.next() |
| 89 | + if node.Right != nil { |
| 90 | + q = append(q, node.Right) |
| 91 | + } |
| 92 | + } |
| 93 | + return root |
| 94 | +} |
| 95 | + |
| 96 | +type stream struct { |
| 97 | + arr []string |
| 98 | + cur int |
| 99 | +} |
| 100 | + |
| 101 | +func newStream(data string) (*stream, bool) { |
| 102 | + if len(data) == 0 { |
| 103 | + return nil, false |
| 104 | + } |
| 105 | + arr := strings.Split(data, ",") |
| 106 | + if len(arr) == 0 { |
| 107 | + return nil, false |
| 108 | + } |
| 109 | + return &stream{ |
| 110 | + arr: arr, |
| 111 | + cur: 0, |
| 112 | + }, true |
| 113 | +} |
| 114 | + |
| 115 | +func (s *stream) empty() bool { |
| 116 | + return s.cur == len(s.arr) |
| 117 | +} |
| 118 | + |
| 119 | +func (s *stream) next() *TreeNode { |
| 120 | + data := s.arr[s.cur] |
| 121 | + s.cur++ |
| 122 | + return s.node(data) |
| 123 | +} |
| 124 | + |
| 125 | +func (s *stream) node(data string) *TreeNode { |
| 126 | + if data == "nil" { |
| 127 | + return nil |
| 128 | + } |
| 129 | + v, _ := strconv.Atoi(data) |
| 130 | + return &TreeNode{Val: v} |
| 131 | +} |
| 132 | + |
| 133 | +type TreeNode struct { |
| 134 | + Val int |
| 135 | + Right *TreeNode |
| 136 | + Left *TreeNode |
| 137 | +} |
0 commit comments