Serialize and Deserialize N-ary Tree
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 408 ms)β
| Metric | Value |
|---|---|
| Runtime | 408 ms |
| Memory | 51.9 MB |
| Date | 2020-01-27 |
Solution
/*
// Definition for a Node.
public class Node {
public int val;
public IList<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, IList<Node> _children) {
val = _val;
children = _children;
}
}
*/
public class Codec {
// Encodes a tree to a single string.
public string serialize(Node root)
{
List<string> nodes = new List<string>();
SerializeHelper(root, nodes);
return String.Join(",",nodes);
}
public void SerializeHelper(Node root, List<string> nodes)
{
if (root == null) return;
nodes.Add($"{root.val}");
nodes.Add($"{root.children.Count}");
foreach (var child in root.children)
{
SerializeHelper(child, nodes);
}
}
// Decodes your encoded data to tree.
public Node deserialize(string data)
{
if(string.IsNullOrEmpty(data)) return null;
Queue<string> nodes = new Queue<string>(data.Split(','));
return DeserializeHelper(nodes);
}
private Node DeserializeHelper(Queue<string> nodes)
{
Node root = new Node();
root.val = Int32.Parse(nodes.Dequeue());
int size = Int32.Parse(nodes.Dequeue());
root.children = new List<Node>();
for (int i = 0; i < size; i++)
{
root.children.Add(DeserializeHelper(nodes));
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
π 1 more C# submission(s)
Submission (2020-01-27) β 680 ms, 54 MBβ
/*
// Definition for a Node.
public class Node {
public int val;
public IList<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, IList<Node> _children) {
val = _val;
children = _children;
}
}
*/
public class Codec {
// Encodes a tree to a single string.
public string serialize(Node root)
{
List<string> nodes = new List<string>();
SerializeHelper(root, nodes);
return String.Join(",",nodes);
}
public void SerializeHelper(Node root, List<string> nodes)
{
if (root == null) return;
nodes.Add($"{root.val}");
nodes.Add($"{root.children.Count}");
foreach (var child in root.children)
{
SerializeHelper(child, nodes);
}
}
// Decodes your encoded data to tree.
public Node deserialize(string data)
{
if(string.IsNullOrEmpty(data)) return null;
Queue<string> nodes = new Queue<string>(data.Split(','));
return DeserializeHelper(nodes);
}
private Node DeserializeHelper(Queue<string> nodes)
{
Node root = new Node();
root.val = Int32.Parse(nodes.Dequeue());
int size = Int32.Parse(nodes.Dequeue());
root.children = new List<Node>();
for (int i = 0; i < size; i++)
{
root.children.Add(DeserializeHelper(nodes));
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |