算法通关村第五关-二叉树遍历(深度优先)之经典问题: 递归/迭代实现二叉树前、中、后序遍历

基础知识(青铜挑战)

  • 理解递归思想:调用自己、压栈、明确终止条件

实战训练(白银挑战)

递归实现二叉树的前、中、后序遍历
  • 我的建议是直接把代码背下来

  • 你当然可以尝试去理解,也不是很难想明白,但是不要钻牛角尖了,递归这玩意儿,很不友好

java 复制代码
 /**
     * 前序遍历,将结果返回到list中
     *
     * @param root
     * @param res
     */
​
    public static void preOrder(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        res.add(root.val);
        preOrder(root.left, res);
        preOrder(root.right, res);
    }
​
   
复制代码
 
java 复制代码
 /**
     * 中序遍历,将结果返回到list中
     *
     * @param root
     * @param res
     */
  public static void inOrder(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        inOrder(root.left, res);
        res.add(root.val);
        inOrder(root.right, res);
    }
​
java 复制代码
  /**
     * 后序遍历,将结果返回到list中
     *
     * @param root
     * @param res
     */
    public static void postOrder(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        postOrder(root.left, res);
        postOrder(root.right, res);
        res.add(root.val);
    }
迭代实现二叉树的前、中、后序遍历
前序遍历
java 复制代码
   public static List<Integer> preOrderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if (root == null) {
            return res;
        }
​
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
        TreeNode node = root;
        while (!stack.isEmpty() || node != null) {
            while (node != null) {
                res.add(node.val);
                stack.push(node);
                node = node.left;
            }
​
            node = stack.pop();
            node = node.right;
        }
        return res;
    } 
复制代码
  
中序遍历(2023/09/12午)
java 复制代码
public static List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
​
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
​
            root = stack.pop();
            res.add(root.val);
            root = root.right;
        }
        return res;
    }
  • 前序遍历和中序遍历的代码不复杂,还是注意:挺不好理解的,不要钻牛角尖,那么我们接下来看下后序遍历

后序遍历
  • 这个相较于前两个更难实现,但可以这样想:

    • 前序遍历的顺序是:中、左、右,我们简单该两个参数,实现遍历顺序为中、右、左

    • 再将遍历结果reserve反转,不就得到左、右、中的遍历顺序了吗?

java 复制代码
  public static List<Integer> postOrderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
​
        while (!stack.isEmpty() || node != null) {
            while (node != null) {
                res.add(node.val);
                stack.push(node);
                node = node.right;
            }
            node = stack.pop();
            node = node.left;
        }
        Collections.reverse(res);
        return res;
    }

通关(过关挑战)

理解中序遍历的递归过程,挺难的,努努力吧(2023/09/12午)
相关推荐
OxYGC1 分钟前
[AI工程] Spring AI 第廿一篇:存量 REST 接口的 MCP 化改造实录——工具从哪条路径注册、参数描述怎么写、身份怎么过去
java·人工智能·spring
晓天衡宇•评测社区5 分钟前
晓天衡宇亮相 2026 云栖大会:迈向更可信的高质量评测
人工智能·算法·机器学习
CQU_JIAKE12 分钟前
9.12[a]
算法
CQU_JIAKE22 分钟前
9.11【A】
算法
Zhou14113625 分钟前
SpringBoot_02_自动配置原理
java·spring boot·后端
FYKJ_201031 分钟前
SSM校园失物招领系统41452-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·typescript·spark
kv11035 分钟前
Android studio国内开发有关
java·数据库·android studio
All for pursuit.1 小时前
【分治-3】347.前K个高频元素
数据结构·c++·算法·leetcode
卓怡学长1 小时前
w198基于springboot始于足下健康打卡平台的设计与实现
java·spring boot·spring·intellij-idea
索尔~古德曼1 小时前
CF(1400-1400&1300-1500)错题集
算法