zoukankan      html  css  js  c++  java
  • 非递归遍历

    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; }
    }
    

    以免忘记
  • 相关阅读:
    Java 的类加载顺序
    单链表之一元多项式求和C++实现
    顺序线性表之大整数求和C++实现
    线性表之单链表C++实现
    NOIP 2009 潜伏者
    JDOJ 2782: 和之和
    浅谈前、中、后缀表达式
    CF13B Letter A
    洛谷 P5015 标题统计
    NOIP 2013 转圈游戏
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1734453.html
Copyright © 2011-2022 走看看