二叉树非递归遍历方法

直接上代码


import java.util.LinkedList;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class erchashubianli {

    // 递归前序遍历
    public static void preOrder1(TreeNode node) {

        if (node == null)
            return;
        System.out.print(node.val + " ");
        postOrder1(node.left);
        postOrder1(node.right);

    }

    // 非递归前序遍历
    public static void preOrder2(TreeNode node) {

        LinkedList list = new LinkedList<>();

        TreeNode p = node;

        while (p != null || !list.isEmpty()) {
            while (p != null) {

                System.out.print(p.val + " ");
                list.add(p);
                p = p.left;
            }
            if (!list.isEmpty()) {
                p = list.removeLast();
                p = p.right;
            }

        }
    }

    // 递归中序遍历
    public static void midOrder1(TreeNode node) {

        if (node == null)
            return;
        midOrder1(node.left);
        System.out.print(node.val + " ");
        midOrder1(node.right);

    }

    // 非递归中序遍历
    public static void midOrder2(TreeNode node) {

        LinkedList list = new LinkedList<>();

        TreeNode p = node;

        while (p != null || !list.isEmpty()) {
            while (p != null) {

                list.add(p);
                p = p.left;
            }
            if (!list.isEmpty()) {
                p = list.removeLast();
                System.out.print(p.val + " ");
                p = p.right;
            }

        }
    }

    // 递归后序遍历
    public static void postOrder1(TreeNode node) {

        if (node == null)
            return;
        postOrder1(node.left);
        postOrder1(node.right);
        System.out.print(node.val + " ");

    }

    // 非递归后序遍历
    public static void postOrder2(TreeNode node) {

        class Temp {
            boolean isFirst;
            TreeNode node;

            public Temp(TreeNode node, boolean isFirst) {
                // TODO Auto-generated constructor stub
                this.isFirst = isFirst;
                this.node = node;
            }
        }
        LinkedList list = new LinkedList<>();
        TreeNode p = node;

        while (p != null || !list.isEmpty()) {
            while (p != null) {

                Temp tmp = new Temp(p, true);
                list.add(tmp);
                p = p.left;
            }
            if (!list.isEmpty()) {

                Temp tmp = list.removeLast();

                if (tmp.isFirst) {
                    p = tmp.node.right;
                    tmp.isFirst = false;
                    list.add(tmp);
                } else {

                    p = null;
                    System.out.print(tmp.node.val + " ");
                }
            }
        }
    }

    public static void main(String[] args) {

        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);

        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node5.right = node6;

        // 递归前序遍历
        preOrder1(node1);
        // 非递归前序遍历
        System.out.println();
        preOrder2(node1);
        // 递归中序遍历
        System.out.println();
        midOrder1(node1);
        // 非递归中序遍历
        System.out.println();
        midOrder2(node1);
        // 递归后序遍历
        System.out.println();
        postOrder1(node1);
        // 非递归后序遍历
        System.out.println();
        postOrder2(node1);

    }
}

你可能感兴趣的:(二叉树,非递归,二叉树遍历,编程题)