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


    基础知识(青铜挑战)

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

    实战训练(白银挑战)

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

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

    1. /**
    2.     * 前序遍历,将结果返回到list中
    3.     *
    4.     * @param root
    5.     * @param res
    6.     */
    7.    public static void preOrder(TreeNode root, List res) {
    8.        if (root == null) {
    9.            return;
    10.       }
    11.        res.add(root.val);
    12.        preOrder(root.left, res);
    13.        preOrder(root.right, res);
    14.   }
    15.  
     
    
    1. /**
    2.     * 中序遍历,将结果返回到list中
    3.     *
    4.     * @param root
    5.     * @param res
    6.     */
    7.  public static void inOrder(TreeNode root, List res) {
    8.        if (root == null) {
    9.            return;
    10.       }
    11.        inOrder(root.left, res);
    12.        res.add(root.val);
    13.        inOrder(root.right, res);
    14.   }
    1.  /**
    2.     * 后序遍历,将结果返回到list中
    3.     *
    4.     * @param root
    5.     * @param res
    6.     */
    7.    public static void postOrder(TreeNode root, List res) {
    8.        if (root == null) {
    9.            return;
    10.       }
    11.        postOrder(root.left, res);
    12.        postOrder(root.right, res);
    13.        res.add(root.val);
    14.   }
    迭代实现二叉树的前、中、后序遍历
    1.   public static List preOrderTraversal(TreeNode root) {
    2.        List res = new ArrayList();
    3.        if (root == null) {
    4.            return res;
    5.       }
    6.        Deque stack = new LinkedList();
    7.        TreeNode node = root;
    8.        while (!stack.isEmpty() || node != null) {
    9.            while (node != null) {
    10.                res.add(node.val);
    11.                stack.push(node);
    12.                node = node.left;
    13.           }
    14.            node = stack.pop();
    15.            node = node.right;
    16.       }
    17.        return res;
    18.   }
      
    1. public static List inorderTraversal(TreeNode root) {
    2.        List res = new ArrayList();
    3.        Deque stack = new LinkedList();
    4.        while (root != null || !stack.isEmpty()) {
    5.            while (root != null) {
    6.                stack.push(root);
    7.                root = root.left;
    8.           }
    9.            root = stack.pop();
    10.            res.add(root.val);
    11.            root = root.right;
    12.       }
    13.        return res;
    14.   }
    • 前序遍历和中序遍历的代码不复杂,还是注意:挺不好理解的,不要钻牛角尖,那么我们接下来看下后序遍历

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

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

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

    1.  public static List postOrderTraversal(TreeNode root) {
    2.        List res = new ArrayList<>();
    3.        if (root == null) return res;
    4.        Stack stack = new Stack<>();
    5.        TreeNode node = root;
    6.        while (!stack.isEmpty() || node != null) {
    7.            while (node != null) {
    8.                res.add(node.val);
    9.                stack.push(node);
    10.                node = node.right;
    11.           }
    12.            node = stack.pop();
    13.            node = node.left;
    14.       }
    15.        Collections.reverse(res);
    16.        return res;
    17.   }

    通关(过关挑战)

    • 理解中序遍历的递归过程,挺难的,努努力吧(2023/09/12午)
  • 相关阅读:
    linux-进程管理
    马斯克热搜体质无疑,称已将大脑上传云端,却遭网友热议!
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统功能清单
    git manual
    已加密的PDF怎么解密?只要学会这两招即可轻松解密
    JDK源码剖析之PriorityQueue优先级队列
    测试用例:四步测试设计法
    2380. 二进制字符串重新安排顺序需要的时间 贪心
    取消检验批过账(取消检验批UD判定到Rerel,再把非限性库存转到质检库存,然后就可以取101收货了)
    Visual Studio导入libtorch(Cuda版)
  • 原文地址:https://blog.csdn.net/m0_62570784/article/details/133460008