class Program { static void Main(string[] args) { } //TreeNode node1 = new TreeNode(); //node1.Name = "a"; //IList<TreeNode> list = new List<TreeNode>(); //for (int i = 0; i < 3; i++) //{ // TreeNode node = new TreeNode() { Name = "TreeNode" + i }; // list.Add(node); // node.List = new List<TreeNode>(); // for (int j = 0; j < 3; j++) // { // TreeNode node2 = new TreeNode() { Name = "TreeNode" + i + "//" + j }; // node.List.Add(node2); // node2.List = new List<TreeNode>(); // node2.List.Add(new TreeNode(){ Name="xxx"}); // } //} //node1.List = list; //FindNode(node1); private static int Step = 0; static IList<TreeNode> list = new List<TreeNode>(); public static void FindNode(TreeNode node) { IList<TreeNode> templist=null; TreeNode currentNode = node; Queue<TreeNode> queue = new Queue<TreeNode>(); switch (Step) { case 0: list.Add(currentNode); templist = currentNode.List; goto case 1; case 1: if (templist != null) { foreach (var item in templist) { queue.Enqueue(item); Console.WriteLine(item.Name); } } goto case 2; case 2: if (queue.Count > 0) { currentNode = queue.Dequeue(); goto case 0; } break; } } } public class TreeNode { public string Name { get; set; } public IList<TreeNode> List { get; set; } }
以免忘记