I have a question about the merits of two different approaches to implementing a recursive method. I've always followed the approach of version 1, i.e., accepting a single Node parameter, but I recently encountered the style used in version 2, which accepts a collection of Nodes.
Consider the following Node class, along with the 2 versions of the Visit method:
class Node
{
public List<Node> children = new List<Node>();
// other data members
}
Version 1 accepts a single Node parameter:
Visit(Node n)
{
DoSomethingUsefulWith(n);
foreach (Node child in n.children)
Visit(child);
}
Version 2 accepts a collection of Nodes:
Visit(List<Node> nodes)
{
foreach (Node n in nodes)
{
DoSomethingUsefulWith(n);
Visit(n.children);
}
}
Are there any benefits, even stylistically, to using one form over the other? Should the choice be based solely on whether you're starting with a single Node vs a collection of Nodes, even though it would be trivial to use either method version in either case?