【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;

}
相关推荐
flashlight_hi1 小时前
LeetCode 分类刷题:199. 二叉树的右视图
javascript·算法·leetcode
LYFlied1 小时前
【每日算法】LeetCode 46. 全排列
前端·算法·leetcode·面试·职场和发展
LYFlied3 小时前
【每日算法】131. 分割回文串
前端·数据结构·算法·leetcode·面试·职场和发展
长安er3 小时前
LeetCode 300/152/416/32 动态规划进阶题型总结(最长递增子序列→最长有效括号)
数据结构·算法·leetcode·动态规划·剪枝
季远迩3 小时前
LeetCode 热题 100 Python3易懂题解(更新中)
算法·leetcode·哈希算法
Dream it possible!3 小时前
LeetCode 面试经典 150_回溯_组合(99_77_C++_中等)
c++·leetcode·面试·回溯
好易学·数据结构4 小时前
可视化图解算法74:最小花费爬楼梯
数据结构·算法·leetcode·动态规划·力扣
Maỿbe4 小时前
力扣hot图论部分
算法·leetcode·图论
LYFlied5 小时前
【每日算法】LeetCode 78. 子集
数据结构·算法·leetcode·面试·职场和发展
月明长歌5 小时前
【码道初阶】【Leetcode606】二叉树转字符串:前序遍历 + 括号精简规则,一次递归搞定
java·数据结构·算法·leetcode·二叉树