二叉树的前/中/后序遍历

1 前序遍历

    // 前序遍历Recursively
    public void preorderTraversal_R(TreeNode root, ArrayList<Integer> aList) {
        if (root == null)
            return;
        aList.add(root.val);
        if (root.left != null) {
            preorderTraversal_R(root.left, aList);
        }
        if (root.right != null) {
            preorderTraversal_R(root.right, aList);
        }
    }
    // 前序遍历Iteratively
    public void preorderTraversal_I(TreeNode root, ArrayList<Integer> aList) {
        if (root == null)
            return;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode p = root;
        while (p != null || !stack.isEmpty()) {
            if (p != null) {
                aList.add(p.val);
                stack.push(p);
                p = p.left;
            } else {
                p = stack.pop();
                p = p.right;
            }
        }
    }

2 中序遍历

    // 中序遍历Recursively
    public void inorderTraversal_R(TreeNode root, ArrayList<Integer> aList) {
        if (root == null)
            return;
        if (root.left != null) {
            inorderTraversal_R(root.left, aList);
        }
        aList.add(root.val);
        if (root.right != null) {
            inorderTraversal_R(root.right, aList);
        }
    }
    // 中序遍历Iteratively
    public void inorderTraversal_I(TreeNode root, ArrayList<Integer> aList) {
        if (root == null)
            return;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode p = root;
        while (p != null || !stack.isEmpty()) {
            if (p != null) {
                stack.push(p);
                p = p.left;
            } else {
                p = stack.pop();
                aList.add(p.val);
                p = p.right;
            }
        }
    }

3 后续遍历

    // 后序遍历Recursively
    public void postorderTraversal_R(TreeNode root, ArrayList<Integer> aList) {
        if (root == null)
            return;
        if (root.left != null) {
            postorderTraversal_R(root.left, aList);
        }
        if (root.right != null) {
            postorderTraversal_R(root.right, aList);
        }
        aList.add(root.val);
    }
    // 后序遍历Iteratively
    // 需要判断左右子树都遍历过了,才能输出跟节点的val
    public void postorderTraversal_I(TreeNode root, ArrayList<Integer> aList) {
        if (root == null)
            return;
        TreeNode p = root;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        HashMap<TreeNode, Integer> hMap = new HashMap<TreeNode, Integer>();
        while(p != null || !stack.isEmpty()){
            while(p != null){
                p.isFirst = true;
                stack.push(p);
                p = p.left;
            }

            if(!stack.empty()){
                p = stack.pop();
                if(p.isFirst){
                    p.isFirst = false;
                    stack.push(p);
                    p = p.right;
                }
                else {
                    aList.add(p.val);
                    p = null;
                }
            }
        }

4 运行结果

    public static void main(String[] args) {
        /* * 建立一颗二叉树 * 3 * / \ * 9 20 * / \ * 15 7 **/

        TreeNode root = new TreeNode(3);
        TreeNode l = new TreeNode(9);
        TreeNode r = new TreeNode(20);
        TreeNode rl = new TreeNode(15);
        TreeNode rr = new TreeNode(7);
        root.left = l;
        root.right = r;
        r.left = rl;
        r.right = rr;

        System.out.println("中序遍历Recursively:");
        aList.clear();
        sol.inorderTraversal_R(root, aList);
        for (Integer integer : aList) {
            System.out.println(integer);
        }

        System.out.println("中序遍历Iteratively:");
        sol.inorderTraversal_I(root, aList);
        for (Integer integer : aList) {
            System.out.println(integer);
        }

        System.out.println("前序遍历Recursively:");
        aList.clear();
        sol.preorderTraversal_R(root, aList);
        for (Integer integer : aList) {
            System.out.println(integer);
        }

        System.out.println("前序遍历Iteratively:");
        aList.clear();
        sol.preorderTraversal_I(root, aList);
        for (Integer integer : aList) {
            System.out.println(integer);
        }

        System.out.println("后序遍历Recursively:");
        aList.clear();
        sol.postorderTraversal_R(root, aList);
        for (Integer integer : aList) {
            System.out.println(integer);
        }

        System.out.println("后序遍历Iteratively:");
        aList.clear();
        sol.postorderTraversal_I(root, aList);
        for (Integer integer : aList) {
            System.out.println(integer);
        }

二叉树的前/中/后序遍历_第1张图片

二叉树的前/中/后序遍历_第2张图片

你可能感兴趣的:(java,遍历,二叉树)