二叉树遍历的非递归写法

层次遍历,也就是我们常说的广度优先遍历

这个相对来说比较简单,就是一层一层遍历,核心点是采用队列,queue

直接上代码

   public static void levelTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            System.out.println(poll.value);
            if (poll.left != null) {
                queue.offer(poll.left);
            }
            if (poll.right != null) {
                queue.offer(poll.right);
            }
        }
    }

接下来是深度优先遍历,深度优先遍历分为三种,先序遍历,中序遍历,后续遍历

而深度优先遍历,核心点是采用栈

先序遍历的非递归写法,大致有两种,一种是投机取巧,一种是标准写法

直接上代码,投机取巧的写法

   public static void preorderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            System.out.println(pop.value);
            if (pop.right != null) {
                stack.push(pop.right);
            }
            if (pop.left != null) {
                stack.push(pop.left);
            }
        }
    }

标准写法

   public static void preorderTraversal02(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode temp = root;
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                System.out.println(temp.value);
                temp = temp.left;
            }
            if (!stack.isEmpty()) {
                TreeNode pop = stack.pop();
                temp = pop.right;
            }
        }
    }

然后是中序遍历,中序遍历没有投机取巧的方法,只有一种标准写法

 public static void middleorderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode temp = root;
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                temp = temp.left;
            }
            if (!stack.isEmpty()) {
                TreeNode pop = stack.pop();
                System.out.println(pop.value);
                temp = pop.right;
            }
        }
    }

后序遍历,也有两种写法,一种投机取巧,一种标准写法

投机取巧的方式,代码如下

 public static void postorderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        List<TreeNode> list = new LinkedList<>();
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            list.add(pop);
            if (pop.left != null) {
                stack.push(pop.left);
            }
            if (pop.right != null) {
                stack.push(pop.right);
            }
        }
        Collections.reverse(list);
        list.stream().forEach(System.out::println);
    }

标准写法

  public static void postorderTraversal02(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode temp = root, lastNode = null;
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                temp = temp.left;
            }
            // 查看当前栈顶元素
            temp = stack.peek();
            // 如果其右子树也为空,或者右子树已经访问过,则可以直接输出当前节点的值
            if (temp.right == null || temp.right == lastNode) {
                System.out.println(temp.value);
                stack.pop();
                // 把输出的节点赋值给lastNode游标,作为下次比对的依据
                lastNode = temp;
                temp = null;
            } else {
                // 否则,继续遍历右子树
                temp = temp.right;
            }
        }
    }

总结:

广度优先遍历比较简单,深度优先遍历,先序与中序标准写法是相同的代码结构,只需要一个指针,后续则不一样,需要记录上一次的遍历节点,需要两个指针,总的来说,练习一下都比较简单。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值