Non-recursive binary tree traversal after preamble of

Recursive way to achieve binary tree traversal after the order before it is simple, we only need to use a very simple recursive statements, function stack will automatically help us to perform complex operations. Using non-recursive manner of a binary tree in front postorder traversal, essentially create a stack to simulate human function stack.

1. preorder traversal non-recursive

  1. A stack application stack, and the stack is pressed into the head node.

  2. Pop stack node from the stack, the print, then its right child node (not empty then) be pressed into the stack, and finally to its left child node (if not empty) pressed into the stack.

  3. Step 2 is repeated until the stack is empty, the entire process ends.

private static void firstSearch(TreeNode t){

        Stack<TreeNode> stack = new Stack<>();
        // 先放根,然后pop出来放右节点和左节点,然后重复上述操作
        stack.push(t);
        while(!stack.isEmpty()){
            TreeNode temp = stack.peek();
            stack.pop();
            System.out.println(temp.val);
            if(temp.right != null){
                stack.push(temp.right);
            }
            if(temp.left != null){
                stack.push(temp.left);
            }
        }
    }

2. In order to achieve non-recursive traversal

  1. Apply a stack stack, initial seasonal cur = head

  2. Cur first pushed onto the stack, the left border are sequentially pushed onto the stack, i.e., kept to make t = t.left, repeat steps 2

  3. 2 repeated until is null, a pop node from the stack, referred to as a node, the node value of the print, and let t = node.right, repeat steps 2

  4. When the stack is empty and t is empty, the process stops.


private static void midSearch(TreeNode t){
        Stack<TreeNode> stack = new Stack<>();
        // 首先不断入栈顶节点的左节点,直到入不了为止,然后pop出栈顶节点,入一个右节点,然后入该右节点的左子节点直到
        // 入不了为止,重复上述步骤
        if(t != null){
            while(t != null || !stack.isEmpty()){
                if(t != null){
                    stack.push(t);
                    t = t.left;
                }else{
                    System.out.println(stack.peek().val);
                    t = stack.pop().right;
                }
            }
        }
    }

3. Non-postorder recursive

  1. Apply a stack s1, then the head node onto the stack of s1.

  2. From the pop-s1 nodes recorded as temp, temp and then turn left child node and the right child node s1 pushed in.

  3. Throughout the process, each node is ejected from s1 and s2 are put.

  4. Repeating steps 2 and 3 until s1 is empty, the process stops.

  5. Sequentially ejected from the print node and the s2, the print order after the order is traversing.


private static void lastSearch(TreeNode t){
        // 仿照先序遍历的相反顺序,但是pop出来后需要放入另一个栈中
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(t);
        while(!stack1.isEmpty()){
            TreeNode temp = stack1.peek();
            stack2.push(stack1.pop());
            if(temp.left != null){
                stack1.push(temp.left);
            }
            if(temp.right != null){
                stack1.push(temp.right);
            }
        }
        while(!stack2.isEmpty()){
            System.out.println(stack2.pop().val);
        }
    }

Guess you like

Origin www.cnblogs.com/Water2Wine/p/12580860.html