Help me understand Inorder Traversal without using recursion

后端 未结 14 2647
别跟我提以往
别跟我提以往 2020-12-12 11:44

I am able to understand preorder traversal without using recursion, but I\'m having a hard time with inorder traversal. I just don\'t seem to get it, perhaps, because I have

14条回答
  •  囚心锁ツ
    2020-12-12 12:44

    Here is a sample of in order traversal using stack in c# (.net):

    (for post order iterative you may refer to: Post order traversal of binary tree without recursion)

    public string InOrderIterative()
            {
                List nodes = new List();
                if (null != this._root)
                {
                    Stack stack = new Stack();
                    var iterativeNode = this._root;
                    while(iterativeNode != null)
                    {
                        stack.Push(iterativeNode);
                        iterativeNode = iterativeNode.Left;
                    }
                    while(stack.Count > 0)
                    {
                        iterativeNode = stack.Pop();
                        nodes.Add(iterativeNode.Element);
                        if(iterativeNode.Right != null)
                        {
                            stack.Push(iterativeNode.Right);
                            iterativeNode = iterativeNode.Right.Left;
                            while(iterativeNode != null)
                            {
                                stack.Push(iterativeNode);
                                iterativeNode = iterativeNode.Left;
                            }
                        }
                    }
                }
                return this.ListToString(nodes);
            }
    

    Here is a sample with visited flag:

    public string InorderIterative_VisitedFlag()
            {
                List nodes = new List();
                if (null != this._root)
                {
                    Stack stack = new Stack();
                    BinaryTreeNode iterativeNode = null;
                    stack.Push(this._root);
                    while(stack.Count > 0)
                    {
                        iterativeNode = stack.Pop();
                        if(iterativeNode.visted)
                        {
                            iterativeNode.visted = false;
                            nodes.Add(iterativeNode.Element);
                        }
                        else
                        {
                            iterativeNode.visted = true;
                            if(iterativeNode.Right != null)
                            {
                                stack.Push(iterativeNode.Right);
                            }
                            stack.Push(iterativeNode);
                            if (iterativeNode.Left != null)
                            {
                                stack.Push(iterativeNode.Left);
                            }
                        }
                    }
                }
                return this.ListToString(nodes);
            }
    

    the definitions of the binarytreenode, listtostring utility:

    string ListToString(List list)
            {
                string s = string.Join(", ", list);
                return s;
            }
    
    
    class BinaryTreeNode
        {
            public int Element;
            public BinaryTreeNode Left;
            public BinaryTreeNode Right;        
        }
    

提交回复
热议问题