Exemple #1
0
        public List <int> IterativePostOrderTraversal(IterativePostOrderTraversalNode node)
        {
            Stack <IterativePostOrderTraversalNode> s = new Stack <IterativePostOrderTraversalNode>();

            if (node == null)
            {
                return(list);
            }

            s.Push(node);

            IterativePostOrderTraversalNode prev = null;

            while (s.Count != 0)
            {
                IterativePostOrderTraversalNode current = s.Peek();

                if (prev == null || prev.left == current || prev.right == current)
                {
                    if (current.left != null)
                    {
                        s.Push(current.left);
                    }
                    else if (current.right != null)
                    {
                        s.Push(current.right);
                    }
                    else
                    {
                        s.Pop();
                        list.Add(current.data);
                    }
                }
                else if (current.left == prev)
                {
                    if (current.right != null)
                    {
                        s.Push(current.right);
                    }
                    else
                    {
                        s.Pop();
                        list.Add(current.data);
                    }
                }
                else if (current.right == prev)
                {
                    s.Pop();
                    list.Add(current.data);
                }

                prev = current;
            }

            return(list);
        }
Exemple #2
0
 public IterativePostOrderTraversalNode(int item)
 {
     data = item;
     left = right;
 }