【五一专栏】1. 迭代版二叉树的前、中、后序遍历

简介: 【五一专栏】1. 迭代版二叉树的前、中、后序遍历

前序遍历

思路

  • 对于前序来说,遍历的时候访问顺序是:中 - 左 - 右
  • 我们在进行迭代的时候,利用的是 Stack(先进后出) 这种数据结构
  • 我们遍历的顺序如下图所示:
  • 因为我们的出栈顺序为:左 右,所以压栈的顺序为:右 左

题目代码

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root == null){
            return list;
        }
        stack.add(root);
        // 中左右 
        // 压栈:右左
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            list.add(node.val);
            if(node.right != null){
                stack.add(node.right);
            }
            if(node.left != null){
                stack.add(node.left);
            }
        }
        return list;
    }
}

中序遍历

思路

  • 对于中序遍历来说,我们遍历的顺序是:左 - 中 - 右
  • 我们使用的数据结构还是 Stack
  • 我们遍历的顺序如下图所示:
  • 终止而言,对于这种递归的操作,不要去思考全局,要做好当前的操作,再去递归。

题目代码

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root == null){
            return list;
        }
        while(!stack.isEmpty() || root != null){
            while(root != null){
                stack.add(root);
                root = root.left;
            }
            TreeNode node = stack.pop();
            list.add(node.val);
            root = node.right;
        }
        return list;
    }
}

后序遍历

思考

  • 对于后序遍历,遍历的顺序是:左 - 右 - 中,而前序遍历为:中 - 左 - 右
  • 我们只需要在前序遍历的代码改一下
  • 我们可以思考一下,前序中为什么会有 先添加 root.right 再添加 root.left 的操作?
  • 主要就是在 Stack 弹出的时候,按照这个添加顺序,会优先弹出我们的 root.left 再弹出我们的 root.right,达到 中 - 左 - 右 的顺序
  • 我们假如换一下添加的顺序,比如:先添加 root.left 再添加 root.right,这样的话,弹出的时候,就会按照 中 - 右 - 左 的顺序
  • 可能这时候你还没看出来蹊跷,假如把这个顺序翻转一下,得到 左 - 右 - 后
  • 这是不是就是我们的后序遍历了

题目代码

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root == null){
            return list;
        }
        stack.add(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            list.add(node.val);
            if(node.left != null){
                stack.add(node.left);
            }
            if(node.right != null){
                stack.add(node.right);
            }
        }
        Collections.reverse(list);
        return list;
    }
}


相关文章
|
1月前
二叉树刷题记(二-中序遍历)
二叉树刷题记(二-中序遍历)
|
1月前
|
算法
二叉树刷题记(三-后序遍历)
二叉树刷题记(三-后序遍历)
|
1月前
二叉树刷题记(四-前序遍历)
二叉树刷题记(四-前序遍历)
|
1月前
|
API
【二叉树】练习题终章
【二叉树】练习题终章
36 0
|
10月前
|
存储 算法 C++
婉约而深刻:二叉树的中序遍历之旅
婉约而深刻:二叉树的中序遍历之旅
43 0
|
11月前
|
Cloud Native
【刷题日记】814. 二叉树剪枝
本次刷题日记的第 80 篇,力扣题为:814. 二叉树剪枝
|
1月前
|
算法
刷题专栏(十三):二叉搜索树的最近公共祖先
刷题专栏(十三):二叉搜索树的最近公共祖先
31 0
|
1月前
|
存储 算法
刷题专栏(十一):二叉树的后序遍历
刷题专栏(十一):二叉树的后序遍历
42 0
|
1月前
|
算法
刷题专栏(六):二叉树的最大深度
刷题专栏(六):二叉树的最大深度
40 0
|
10月前
|
程序员
程序员怎能不会二叉树系列(二)二叉树的四种遍历
程序员怎能不会二叉树系列(二)二叉树的四种遍历

热门文章

最新文章