你好,
>>在一个方法内,对树集合进行非递归的广度优先遍历和深度优先遍历,能不在改变树类的方式下,有哪些方法?
可以考虑使用c#的迭代器。下面有个简单的目录树的例子,你可以参考一下:
public static IEnumerable<string> AllFolders(string root)
{
var folders = new Stack<string>();
folders.Push(root);
while (folders.Count > 0)
{
string folder = folders.Pop();
yield return folder;
foreach (var item in Directory.EnumerateDirectories(folder))
folders.Push(item);
}
}
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.