二叉树遍历(递归和非递归)前序、中序、后序遍历

二叉树前序遍历(递归)

/**
     * 二叉树前序遍历(递归)
     * @param root
     */
    public static void recursionBefore(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.getValue() + "->");
        recursionBefore(root.getLeft());
        recursionBefore(root.getRight());
    }

二叉树前序遍历(非递归)

/**
     * 二叉树前序遍历(非递归)
     *前序遍历:根->左->右
     * @param root
     * @return
     */
    public static void beforeTree(TreeNode root){
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        System.out.print("非递归遍历-前序:");
        while (!stack.isEmpty()) {
            root = stack.pop();
            TreeNode left = root.getLeft();
            TreeNode right = root.getRight();
            System.out.print(root.getValue() + " -> ");
            if (right != null) {
                stack.push(right);
            }
            if (left != null) {
                stack.push(left);
            }
        }
        System.out.println();
    }

二叉树中序遍历(递归)

/**
     * 二叉树中序遍历(递归)
     * @param root
     */
    public static void recursionMiddle(TreeNode root) {
        if (root == null) {
            return;
        }
        recursionMiddle(root.getLeft());
        System.out.print(root.getValue() + "->");
        recursionMiddle(root.getRight());
    }

二叉树中序遍历(非递归)

/**
     * 二叉树中序遍历(非递归)
     *中序遍历:左->根->右
     * @param root
     * @return
     */
    public static void middleTree(TreeNode root){
        if (root == null) {
            return;
        }
        System.out.print("非递归遍历-中序:");
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            if (root != null) {
                stack.push(root);
                root = root.getLeft();
            } else {
                root = stack.pop();
                if (root != null) {
                    System.out.print(root.getValue() + " -> ");
                }
                root = root.getRight();
            }
        }
        System.out.println();
        return;
    }

二叉树后序遍历(递归)

/**
     * 二叉树后序遍历(递归)
     * @param root
     */
    public static void recursionAfter(TreeNode root) {
        if (root == null) {
            return;
        }
        recursionAfter(root.getLeft());
        recursionAfter(root.getRight());
        System.out.print(root.getValue() + "->");
    }

二叉树后序遍历-2个栈(非递归)

/**
     * 二叉树后序遍历2个栈:(非递归)
     *后序遍历:(左->右->根)
     * @param root
     * @return
     */
    public static void afterTree(TreeNode root) {
        //把s1,作为栈使用
        Stack<TreeNode> s1 = new Stack<>();
        //把S2,把LinkedList作为栈使用
        Stack<TreeNode> s2 = new Stack<>();
        s1.push(root);
        System.out.print("非递归遍历-后序2个栈:");
        while (!s1.isEmpty()) {
            root = s1.pop();
            s2.push(root);
            TreeNode left = root.getLeft();
            if (left != null) {
                s1.push(left);
            }
            TreeNode right = root.getRight();
            if (right != null) {
                s1.push(right);
            }
        }
        while (!s2.isEmpty()) {
            System.out.print(s2.pop().getValue() + " -> ");
        }
        System.out.println();
    }

二叉树后序遍历-1个栈(非递归)

/**
     * 二叉树后序遍历1个栈:(非递归)
     *后序遍历:(左->右->根)
     * @param root
     * @return
     */
    public static void afterTree2(TreeNode root) {
        //把s1,作为栈使用
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        TreeNode tempNode = null;
        System.out.print("非递归遍历-后序一个栈:");
        while (!stack.isEmpty()) {
            tempNode = stack.peek();
            TreeNode left = tempNode.getLeft();
            TreeNode right = tempNode.getRight();
            if (left != null && root != tempNode.getLeft() && root != tempNode.getRight()) {
                stack.push(left);
            }else if (right != null && root !=tempNode.getRight()) {
                stack.push(right);
            }else{
                System.out.print(stack.pop().getValue() + " -> ");
                root = tempNode;
            }
        }
        System.out.println();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值