-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0297-SerializeandDeserializeBinaryTree.cs
59 lines (52 loc) · 1.74 KB
/
0297-SerializeandDeserializeBinaryTree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//-----------------------------------------------------------------------------
// Runtime: 120ms
// Memory Usage: 32.6 MB
// Link: https://leetcode.com/submissions/detail/378613049/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0297_SerializeandDeserializeBinaryTree
{
private readonly char SPLITTER = ',';
private readonly string NULL_VALUE = "null";
// Encodes a tree to a single string.
public string Serialize(TreeNode root)
{
var sb = new StringBuilder();
Serialize(root, sb);
return sb.ToString();
}
private void Serialize(TreeNode root, StringBuilder sb)
{
if (root == null) sb.Append(NULL_VALUE).Append(SPLITTER);
else
{
sb.Append(root.val.ToString()).Append(SPLITTER);
Serialize(root.left, sb);
Serialize(root.right, sb);
}
}
// Decodes your encoded data to tree.
public TreeNode Deserialize(string data)
{
var splits = data.Split(SPLITTER);
var list = new LinkedList<string>(splits);
return Deserialize(list);
}
private TreeNode Deserialize(LinkedList<string> data)
{
if (data.First.Value == NULL_VALUE)
{
data.Remove(data.First);
return null;
}
var root = new TreeNode(int.Parse(data.First.Value));
data.Remove(data.First);
root.left = Deserialize(data);
root.right = Deserialize(data);
return root;
}
}
}