📜  C# 下树并返回 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:24.180000             🧑  作者: Mango

代码示例1
public static IEnumerable Traverse(T item, Func> childSelector)
{
    var stack = new Stack();
    stack.Push(item);
    while (stack.Any())
    {
        var next = stack.Pop();
        yield return next;
        foreach (var child in childSelector(next))
            stack.Push(child);
    }
}