I have a method that looks like:
public IEnumerable<Node> TraverseTree(Node root)
{
if(root.Children != null)
{
foreach(var item in root.Children)
TraverseTree(item);
}
yield return root;
}
and when I do:
var allItems = TraverseTree(someRootNode).ToList();
I only get the first node. Is it not possible to use recursion when using IEnumerable? It will be nice if I can use IEnumerable so that my linq queries do not fully execute.
Edit
Sorry my Node class looks like:
class Node
{
public Node Val;
public List<Node> Children = new List<Node>();
}