I've done a lot of searching, but none of the existing solutions solve my exact problem. I have a list:
Input[] inputs = new Input[]
{
new Input(1),
new Input(3,1),
new Input(19,3),
new Input(22,1),
new Input(4,1),
new Input(5,22),
};
Here is the declaration for BuildTree() which currently does not work:
public TreeNode BuildTree(IEnumerable<Input> inputs, TreeNode parentNode = null)
{
List<Input> nodes = inputs.ToList<Input>();
foreach (Input node in nodes)
{
TreeNode newNode = new TreeNode(node.Id);
if (parentNode == null)
{
parentNode = BuildTree(nodes, newNode);
}
else
{
parentNode.AddChild(newNode);
}
}
return parentNode;
}
Here is the call to BuildTree:
TreeNode rootNode = BuildTree(inputs);
So the BuildTree function has to return the root of the tree after building it. I've tried looping through the inputs. I've tried removing the first input from the list with each iteration. I can't quite figure it out. Any help would be greatly appreciated! Thank you!