【leetcode】第六章 二叉树part01

递归遍历

144. 二叉树的前序遍历

java 复制代码
// 前序遍历
public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        preOrder(root, res);
        return res;
    }

    private void preOrder(TreeNode root, List<Integer> res) {
        if (root == null) return;
        
        res.add(root.val);
        preOrder(root.left, res);
        preOrder(root.right, res);
    }

94. 二叉树的中序遍历

java 复制代码
public List<Integer> inorderTraversal(TreeNode root) {
        // 递归
        List<Integer> res = new ArrayList<>();
        inOrder(root, res);
        return res;
    }

    private void inOrder(TreeNode root, List<Integer> res) {
        if (root == null) return;

        inOrder(root.left, res);
        res.add(root.val);
        inOrder(root.right, res);
    }

145. 二叉树的后序遍历

java 复制代码
public List<Integer> postorderTraversal(TreeNode root) {
        // 递归
        List<Integer> res = new ArrayList<>();
        postOrder(root, res);
        return res;

    }

    private 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 List<Integer> preorderTraversal(TreeNode root) {
        // 非递归
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            // 一直将左节点压入栈中
            while (root != null) {
                res.add(root.val); // 根节点首先访问
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            root = root.right; 
        }

        return res;
    }
  • 中序
Java 复制代码
public List<Integer> inorderTraversal(TreeNode root) {
        // 非递归
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();

        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;
    }
  • 后序
Java 复制代码
public List<Integer> postorderTraversal(TreeNode root) {
    // 非递归
    List<Integer> res = new ArrayList<>();
    Stack<TreeNode> stack = new Stack<>();
    TreeNode prev = null;
    while (root != null || !stack.isEmpty()) {
        // 将左节点压入栈
        while (root != null) {
            stack.push(root);
            root = root.left;
        }

        root = stack.pop();
        // 后序遍历的过程中在遍历完左子树跟右子树cur都会回到根结点。
        // 所以当前不管是从左子树还是右子树回到根结点都不应该再 操作了,应该退回上层。
        // 如果是从右边再返回根结点,应该回到上层。
        // 主要就是判断出来的是不是右子树,是的话就可以把根节点=加入到list了
        if (root.right == null || root.right == prev) {
            res.add(root.val);
            prev = root;
            root = null;
        }
        else {
            stack.push(root);
            root = root.right;
        }

    }

    return res;

}
相关推荐
罗超驿14 小时前
9.LeetCode 209. 长度最小的子数组 | 滑动窗口专题详解
java·算法·leetcode·面试
水蓝烟雨14 小时前
0135. 分发糖果
算法·leetcode
如竟没有火炬15 小时前
乘法表中第K小的数——二分
开发语言·数据结构·python·算法·leetcode·职场和发展·动态规划
诚威_lol_中大努力中17 小时前
Hot-146 LRU(最近最少使用Least Recent Use)缓存
leetcode
x_xbx18 小时前
LeetCode:739. 每日温度
算法·leetcode·职场和发展
圣保罗的大教堂19 小时前
leetcode 3121. 统计特殊字母的数量 II 中等
leetcode
圣保罗的大教堂19 小时前
leetcode 3120. 统计特殊字母的数量 I 简单
leetcode
sheeta199819 小时前
LeetCode 每日一题笔记 日期:2026.05.28 题目:3093. 最长公共后缀查询
linux·笔记·leetcode
菜菜的顾清寒20 小时前
力扣HOT100(36)二分查找-搜索插入位置
数据结构·算法·leetcode
圣保罗的大教堂20 小时前
leetcode 1752. 检查数组是否经排序和轮转得到 简单
leetcode