No, unfortunately, there is no way to achieve this.
There is no such thing as yield return many GetNodes(...);
and mixing yield return
and normal return
is also not allowed.
However, you can achieve the desired result (deferred execution) using LINQ:
private static IEnumerable<Node> GetNodes(Node root)
{
var result = Enumerable.Empty<Node>();
if (root.left != null)
result = result.Concat(GetNodes(root.left));
result = result.Concat(new [] { root });
if (root.right != null)
result = result.Concat(GetNodes(root.right));
return result;
}
Whether this is better than what you currently have is debatable.