After the first binary tree traversal order non-recursive and recursive

Title Description

Order to achieve the first binary tree, in sequence, after traversal, comprising recursively and non-recursively

achieve

Recursively

public static class Node {
    int val;
    Node left;
    Node right;

    public Node(int data) {
        this.val = data;
    }

}

// 前序递归
public static void preTraversal(Node head) {
    if (head == null) return;

    System.out.print(head.val + " ");
    preTraversal(head.left);
    preTraversal(head.right);
}

// 中序递归
public static void inTraversal(Node head) {
    if (head == null) return;
    inTraversal(head.left);
    System.out.print(head.val + " ");
    inTraversal(head.right);
}

// 后序递归
public static void posTraversal(Node head) {
    if (head == null) return;

    posTraversal(head.left);
    posTraversal(head.right);
    System.out.print(head.val + " ");
}



A non-recursively

prologue

Use the stack structure to achieve, attention to be pressed push the right child, the right child after such a stack order.

 // 前序非递归
    public static void preTravelsal1(Node head) {
        if (head == null) return;

        Stack<Node> stack = new Stack<>();
        stack.push(head);
        while (!stack.isEmpty()) {
            head = stack.pop();
            System.out.print(head.val + " ");
            if (head.right != null) stack.push(head.right);
            if (head.left != null) stack.push(head.left);
        }
    }

In order

It is implemented using a stack structure.

  • The current node is not empty, pushed onto the stack, the current node is left child
  • The current node is empty, a take from the stack, the current node is the right child
// 中序非递归
    public static void inTravelsal1(Node head) {
        if (head == null) return;

        Stack<Node> stack = new Stack<>();
        while (!stack.isEmpty() || head != null) {
            if (head != null) {
                stack.push(head);
                head = head.left;
            } else {
                head = stack.pop();
                System.out.print(head.val + " ");
                head = head.right;
            }
        }
    }

After the order

Preorder traversal around the root, root around postorder traversal.
So in a non-recursive version it is very easy to order around the root, root into the left and right order. Then place each time before a preorder traversal of the print output, the current to the second node into the stack: the root right and left.
Finally, the data of the second stack to a pop-up time, which is about the order of the root

// 后序非递归
    public static void posTravelsal(Node head){
        if(head == null) return;

        Stack<Node> s1 = new Stack<>();
        Stack<Node> s2 = new Stack<>();
        s1.push(head);
        while(!s1.isEmpty()){
            head = s1.pop();
            s2.push(head);
            if(head.left != null) s1.push(head.left);
            if(head.right != null) s2.push(head.right);
        }
        while(!s2.isEmpty()){
            System.out.print(s2.pop().val + " ");
        }
    }
Published 89 original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/Dawn510/article/details/105258652