二叉树的前中后序遍历(Leetcode 94&&144&&145)

题目

​​​​​​144. 二叉树的前序遍历

145. 二叉树的后序遍历

94. 二叉树的中序遍历

代码

分为三种方法1)递归遍历;2)迭代遍历;3)统一迭代遍历

递归遍历

java 复制代码
public List<Integer> preorderTraversal(TreeNode root) {
//        递归遍历:根节点、右子树、左子树
        List<Integer> result = new ArrayList<>();
        preoder(result, root);
        return result;

    }
    public void preoder(List<Integer> result, TreeNode root){
        if(root == null){
            return;
        }
        // 前序遍历 根节点、左子树、右子树
        // 中序遍历 左子树、根节点、右子树
       //  后序遍历 左子树、右子树、根节点
        result.add(root.val);
        preoder(result, root.left);
        preoder(result, root.right);
        return;
    }

迭代遍历:使用栈来存放数据

java 复制代码
public List<Integer> preorderTraversal_1(TreeNode root) {
//    迭代遍历 进栈顺序:根节点、右子树、左子树
//     循环结束条件:栈为空
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> out = new ArrayList<>();
//      对空节点进行判断!
        if(root == null){
            return out;
        }
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode temp  = stack.pop();
            out.add(temp.val);
            if(temp.right != null){
                stack.push(temp.right);
            }
            if(temp.left != null){
                stack.push(temp.left);
            }
        }
        return out;

    }

public List<Integer> inorderTraversal(TreeNode root) {
//        迭代遍历 先找到整棵树最左下角的节点 出栈后考虑该节点的右边子节点
//        有点子巧妙的 自己写的把找最左的节点和出栈循环分开写了 这样就会进入死循环
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> out = new ArrayList<>();
        if(root == null){
            return out;
        }
        TreeNode temp = root;
        while (temp != null || !stack.isEmpty()){
            if(temp != null){
                stack.push(temp);
                temp = temp.left;
            }
            else {
                temp = stack.pop();
                out.add(temp.val);
                temp = temp.right;
            }
        }
        return out;

    }
public List<Integer> postorderTraversal(TreeNode root) {
//        迭代遍历 将前序遍历修改左右顺序再颠倒out数组 就是后序遍历
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> out = new ArrayList<>();
//      对空节点进行判断!
        if(root == null){
            return out;
        }
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode temp  = stack.pop();
//           中、左、右 -> 中、右、左
            out.add(temp.val);
            if(temp.left != null){
                stack.push(temp.left);
            }
            if(temp.right != null){
                stack.push(temp.right);
            }
        }
//        翻转
        Collections.reverse(out);
        return out;

    }

统一迭代:使用null来辅助进行标记

java 复制代码
public List<Integer> preorderTraversal_2(TreeNode root) {
//        统一格式进行迭代遍历 前序遍历 :右节点、左节点、根节点
//        中序遍历 只需要将while中放入节点顺序进行交换:右节点、根节点、左节点
//        后序遍历 :根节点、左节点、右节点

        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root == null){
            return result;
        }
        TreeNode temp = root;
        stack.push(temp);
        while(!stack.isEmpty()){
            temp = stack.pop();
            if(temp != null){
                // 按右、左、根节点的顺序放入栈中
                if(temp.right != null){
                    stack.push(temp.right);
                }

                if(temp.left != null){
                    stack.push(temp.left);
                }
//                将当前节点使用一个null节点进行标记
                stack.push(temp);
                stack.push(null);
            }
            else {
//                当前节点为null 即将下一节点放在result数组中
                result.add(stack.pop().val);
            }
        }
        return result;

    }
相关推荐
Navigator_Z14 小时前
LeetCode //C - 1252. Cells with Odd Values in a Matrix
c语言·算法·leetcode
语戚15 小时前
力扣 1621. 大小为K的不重叠线段的数目:动态规划(Java 实现)
java·算法·leetcode·动态规划·力扣·dp
青山木15 小时前
Hot 100 --- 最长有效括号
java·数据结构·算法·leetcode·动态规划
这料鬼有毒17 小时前
二刷hot100-1143.最长公共子序列
算法·leetcode·动态规划
政企项目老覃18 小时前
边缘 AI 推理部署:安防零售场景下的模型裁剪与端侧落地实践
人工智能·程序人生·算法·性能优化·vllm
GreenTea19 小时前
发布 3 天登顶 HN:不生成一个字的模型 Jev,我把它的源码和黑料都扒了一遍
前端·后端·算法
庖丁解牛20 小时前
无穷小不是一个“神秘小数”,而是一场奔向 0 的过程
算法
ZPC821020 小时前
YOLO26 识别串番茄果梗
人工智能·深度学习·算法·机器学习
景熙552321 小时前
14.Java 集合框架从入门到源码万字详解(含 ArrayList/LinkedList/HashMap 源码、泛型通配符、红黑树)
java·开发语言·数据结构·算法
aaaameliaaa21 小时前
结构体 结构体
c语言·笔记·算法