zoukankan      html  css  js  c++  java
  • C# 逐层反向打印二叉树

    奇数层从左到右打印,偶数层从右到左打印。

    例如:

    a

    b c

    d e f g

    输出则为 a c b d e f g

    View Code
    void SpecialPrintTrees(node root)
    {
        Node temp;
        Stack stack1 = new Stack();
        Stack stack2 = new Stack();
        stack1.Push(root);
        while(stack1.Count>0||stack2.Count>0)
        {
            while(stack1.Count>0)
            {
                temp = stack1.Pop();
                if(temp.Left!=null)
                    Stack2.Push(temp.Left);
                if(temp.Right!=null)
                    Stack2.Push(temp.Right);
                Console.Write(temp.Value);
            }
            while(stack2.Count>0)
            {
                temp = stack2.Pop();
                if(temp.Right!=null)
                    Stack1.Push(temp.Right);
                if(temp.Left!=null)
                    Stack1.Push(temp.Left);
                Console.Write(temp.Value);
            }    
        }
    }
  • 相关阅读:
    HTML5标签
    CTF web之旅 45
    CTF web之旅44
    CTF web之旅 43
    CTF web之旅 42
    CTF web之旅41
    CTF web之旅40
    CTF web之旅 39
    CTF web之旅 38
    CTF web之旅 37
  • 原文地址:https://www.cnblogs.com/Ligeance/p/2946364.html
Copyright © 2011-2022 走看看