Iterate Tree Type Collection

This isn’t really a Unity Specific question but how would iterate through a collection of nodes that are stored in a tree form. For example if I had a node class that holds references to its child nodes and the child nodes held references to their child nodes etc. how could Iterate over them from top down. I believe I need to use a recursive function but can’t really wrap my head around examples I’ve looked at. Any help would be appreciated.

Thanks

I think I’ve found my solution. It wasn’t too hard after all. Queues are easier to wrap my head around than recursive functions.

Queue<Node> queue = new Queue<Node>()
queue.Enqueue(rootNode);

while(queue.Count > 0)
{
	Node currentNode = queue.Dequeue();
	if(currentNode==null)
	{
		continue;
	}
	foreach(Node node in currentNode.Nodes)
	{
		queue.Enqueue(node)
	}
	
	DoSomething(currentNode);
}