The idea is simple—we are using recursion declaration. The task specifies an unlimited number of children, so we are using collection of nodes along with string-to-store additional information about each node.



There are number of possible solutions:



By using class and array:




class Node
{
public Node[] Children;
public string Data;
}


By using struct and array:




struct Node
{
Node[] Children;
string Data;
}


By using class and generic List




class Node
{
public List Children<Node>;
public string Data;
}


Hint: For C# if an access modifier is not specified for a member variable, it has a default access level of private in a class or public in a structure. Pointing this out during the interview process will underline your knowledge and your attention to details.

Think about this questions interviewer might ask: 



What are the difference between class and struct ?
What is the difference between list and array [], and what would be your best choice?
How would you modify this code for a binary tree case?
How would you modify this code to use the generic type instead of string?






Answers and Comments