Skip to main content

Serialize and Deserialize N-ary Tree

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 408 ms)​

MetricValue
Runtime408 ms
Memory51.9 MB
Date2020-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​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed