CSharp · Console
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var tree = new Tree {
Children = new List<Tree> {
new Tree(),
new Tree(),
new Tree {
Children = new List<Tree> {
new Tree()
}
}
}
};
Action<Tree> f = (Tree t) => Console.WriteLine(t.Name);
Dfs(f, tree);
}
public static void Dfs(Action<Tree> f, Tree root) {
f(root);
foreach(var child in root.Children)
Dfs(f, child);
}
public class Tree {
public Guid Name {get;set;}
public IEnumerable<Tree> Children {get;set;}
public Tree()
{
Name = Guid.NewGuid();
Children = new List<Tree>();
}
}
}