Recursion with yield return elements order in tree
问题 I have a recursive function that returns all subtree nodes, given the starting root node. private IEnumerable<Node> getAllNodesRecursively(Node subnode) { foreach (Node node in subnode.Nodes) getAllNodesRecursively(node); yield return subnode; } For the following tree structure: A | +--B | +--C | | | +--D | +--E When I try to iterate as such: foreach (Node n in getAllNodesRecursively(a)) { Console.WriteLine(n); } the function returns the only the A value. I wish to use yield-return with