Binary tree traversal and recursive, non-recursive / traverse the level

Binary tree traversal

Classical binary tree traversal four, preorder traversal, in order traversal, postorder and level traversal.

Wherein, before, during and after the sequence represented by the node to its left and right subtrees of nodes traversed print order.

 

Preorder traversal means that, for any node in the tree, the first printing of this node, and then print it left subtree, and finally print its right subtree.

Preorder means, for any node in the tree, the first print its left subtree, and then print it themselves, and finally print its right subtree.

Postorder means that, for any node in the tree, the first print its left subtree, and then print it right subtree, and finally print the node itself.

 

In subsequent ago DFS (depth-first algorithm) thoughts

 

 

In fact, the binary tree before, during and after order traversal is a recursive process.

Preorder traversal, in fact, is the first to print the root node, then recursively prints the left subtree, and finally the right subtree recursively print. 

前序遍历的递推公式:
preOrder(r) = print r->preOrder(r.left)->preOrder(r.right)

中序遍历的递推公式:
inOrder(r) = inOrder(r.left)->print r->inOrder(r.right)

后序遍历的递推公式:
postOrder(r) = postOrder(r.left)->postOrder(r.right)->print r

 

Recursive

    /**
     * 先序遍历
     */
    public static void preOrder(Node root) {
        if (root == null) {
            return;
        }
        System.out.print(root.value + " -> ");
        preOrder(root.left);
        preOrder(root.right);
    }

    /**
     * 中序遍历
     */
    public static void inOrder(Node root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.value + " -> ");
        inOrder(root.right);
    }

    /**
     * 后序遍历
     */
    public static void postOrder(Node root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.value + " ->  ");
    }

Non-recursive

With the stack itself is a recursive implementation, the non-recursive implementation also need to stack to achieve, in essence, we actually become "Manual recursion"

    /**
     * 前序遍历
     */

    public static void preOrder2(Node root) {
        if (root == null) {
            return;
        }
        Stack<Node> s = new Stack<>();
        s.push(root);
        Node curNode;
        while (!s.isEmpty()) {
            curNode = s.pop();
            System.out.print(curNode.data + "->");
            // 栈先进后出,所以先加入右侧节点,这样输出的时候,先输出左侧节点
            if (curNode.right != null) {
                s.push(curNode.right);
            }
            if (curNode.left != null) {
                s.push(curNode.left);
            }
        }
    }


    /**
     * 中序遍历
     */

    public static void inOrder2(Node root) {
        if (root == null) {
            return;
        }
        Stack<Node> s = new Stack<>();
        Node curNode = root;
        while (!s.isEmpty() || curNode != null) {
            // 入栈所有左节点并输出左节点
            while (curNode != null) {
                s.push(curNode);
                curNode = curNode.left;
            }

            // 弹出左节点
            curNode = s.pop();
            System.out.print(curNode.data + "->");
            // 弹出后,指向当前节点的右节点
            curNode = curNode.right;
        }
    }

     /**
     * 后序遍历
     */
    public static void postOrder2(Node root) {
        if (root == null) {
            return;
        }

        Stack<Node> s1 = new Stack<>();
        Stack<Node> s2 = new Stack<>();

        s1.push(root);

        Node curNode;
        while (!s1.isEmpty()) {

            curNode = s1.pop();
            // 中、右、左顺序压入栈中
            s2.push(curNode);

            // 压入s1为先左后右,保证中、右、左顺序压入s2中
            if (curNode.left != null) {
                s1.push(curNode.left);
            }
            if (curNode.right != null) {
                s1.push(curNode.right);
            }
        }
        while (!s2.isEmpty()) {
            System.out.print(s2.pop().data + "->");
        }

    }

Traverse the level

Level traversal BFS (breadth-first algorithm) thinking.

Level binary tree traversal achieved by means of a general queue.

 

Step traverse the level is:

1. For the node is not empty, the first node is added to the queue

2. out of the team node, the node if the child is not left empty, the children were left to be added to the queue, or do nothing. Similarly right child

3. The above operation is repeated until the queue is empty, the end traverse the level

 

    public static void LevelTraversal(Node root) {
        if (root == null) {
            return;
        }
        Queue<Node> queue = new LinkedList<Node>();
        Node curNode = null;
        queue.offer(root);//将根节点入队
        while (!queue.isEmpty()) {
            curNode = queue.poll();//出队队头元素并访问
            System.out.print(curNode.data + "->");
            if (curNode.left != null)//如果当前节点的左节点不为空入队
            {
                queue.offer(curNode.left);
            }
            if (curNode.right != null)//如果当前节点的右节点不为空,把右节点入队
            {
                queue.offer(curNode.right);
            }
        }
    }

 

Published 113 original articles · won praise 25 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_42006733/article/details/104589720