Java 递归和非递归方式实现二叉树的前、中、后序遍历

Node结点定义

  private static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int value) {
            this.value = value;
        }
    }

前序遍历

递归方式实现

    /**
     * 递归实现前序遍历
     */
    private static void preOrderRecur(Node node) {
        if (node == null) {
            return;
        }
        System.out.print(node.value + " "); //先打印根节点
        preOrderRecur(node.left); //再打印左子树
        preOrderRecur(node.right); //最后打印右子树
    }

非递归方式实现

    /**
     * 非递归实现前序遍历
     *
     * @param node
     */
    private static void preOrderUnRecur(Node node) {
        if (node != null) {
            //定义栈
            Stack<Node> stack = new Stack<>();
            //先放入当前结点
            stack.push(node);
            while (!stack.isEmpty()) {
                node = stack.pop();
                System.out.print(node.value + " ");
                //由于栈是先进后出,所以先放入右子树,再放入左子树
                if (node.right != null) {
                    stack.push(node.right);
                }
                if (node.left != null) {
                    stack.push(node.left);
                }
            }
        }
    }
图文解读

前序非递归遍历

最终结果

5 3 1 4 9 10

中序遍历

递归方式实现

 /**
     * 递归实现中序遍历
     *
     * @param node
     */
    private static void inOrderRecur(Node node) {
        if (node == null) {
            return;
        }
        inOrderRecur(node.left); //先打印左子树
        System.out.print(node.value + " "); //再打印根节点
        inOrderRecur(node.right);//最后打印右子树
    }

非递归方式实现

   /**
     * 非递归实现中序遍历
     *
     * @param node
     */
    private static void inOrderUnRecur(Node node) {
        if (node == null) {
            return;
        }

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

    }
图文解读

在非递归方式实现中序遍历

最终结果

1 3 4 5 9 10

后序遍历

递归方式实现

    /**
     * 递归方式实现后序遍历
     * @param node
     */
    private static void posOrderRecur(Node node) {
        if (node == null) {
            return;
        }
        posOrderRecur(node.left); //先打印左子树
        posOrderRecur(node.right);//再打印右子树
        System.out.print(node.value + " ");//最后打印根结点
    }

非递归方式实现

    /**
     * 非递归方式实现后序遍历
     *
     * @param node
     */
    private static void posOrderUnRecur(Node node) {
        if (node == null) {
            return;
        }
        //
        Stack<Node> stack1 = new Stack<>();
        Stack<Node> stack2 = new Stack<>();
        stack1.push(node);
        while (!stack1.isEmpty()) {
            node = stack1.pop();
            stack2.push(node);
            if (node.left != null) {
                stack1.push(node.left);
            }
            if (node.right != null) {
                stack1.push(node.right);
            }
        }
        while (!stack2.isEmpty()) {
            System.out.print(stack2.pop().value + " ");
        }


    }
图文解读

非递归方式实现后序遍历

最终结果

1 4 3 10 9 5

结语

如果以上文章对您有一点点帮助,希望您不要吝啬的点个赞加个关注,您每一次小小的举动都是我坚持写作的不懈动力!ღ( ´・ᴗ・` )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值