zoukankan      html  css  js  c++  java
  • 后序遍历二叉树-非递归实现

    用栈实现非递归后序遍历二叉树

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication8
    {
        class TreeNode
        {
            public TreeNode(string name, TreeNode right, TreeNode left)
            {
                this.Name = name;
                this.Left = left;
                this.Right = right;
            }
    
            public TreeNode Left { get; set; }
    
            public TreeNode Right { get; set; }
    
            public bool IsVisited { get; set; }
    
            public string Name { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                /*
                1
             |        |
             2        3
           |   |    |   |
          4    7    5   6
             |   |
             8   9
                */
                List<TreeNode> nodeList = new List<TreeNode>();
    
                for (int i = 1; i <= 9; i++)
                {
                    nodeList.Add(new TreeNode($"{i} ", null, null));
                }
    
                nodeList[0].Left = nodeList[1];
                nodeList[0].Right = nodeList[2];
                nodeList[1].Left = nodeList[3];
                nodeList[1].Right = nodeList[6];
                nodeList[2].Left = nodeList[4];
                nodeList[2].Right = nodeList[5];
                nodeList[6].Left = nodeList[7];
                nodeList[6].Right = nodeList[8];
    
                Recurse(nodeList[0]);
            }
    
            static void Recurse(TreeNode node)
            {
                Stack<TreeNode> stack = new Stack<TreeNode>();
                stack.Push(node);
    
                while (stack.Count > 0)
                {
                    var top = stack.Peek();
    
                    if ((top.Right == null && top.Left == null) ||
                        ((top.Left == null || top.Left.IsVisited == true) &&
                        (top.Right == null || top.Right.IsVisited == true))
                        )
                    {
                        top = stack.Pop();
                        Console.Write(top.Name);
                        top.IsVisited = true;
                        continue;
                    }
    
                    if (top.Right != null)
                    {
                        stack.Push(top.Right);
                    }
    
                    if (top.Left != null)
                    {
                        stack.Push(top.Left);
                    }
                }
            }
        }
    }

    结果:

    4 8 9 7 2 5 6 3 1
  • 相关阅读:
    留的住叫做幸福.流逝的叫做遗憾
    我爱你的各国语言
    英语单词 搞笑着背
    爱上你,是我的劫难(转)
    用人的四项基本原则
    希望不会再来 (转)
    8种没结果的爱(未婚者必读)!!!
    留住人才有办法
    英语口语集锦-劝告
    转帖]成功创业家的心理
  • 原文地址:https://www.cnblogs.com/dirichlet/p/8331454.html
Copyright © 2011-2022 走看看