二叉树的前中后序遍历(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;

    }
相关推荐
仰泳的熊猫7 分钟前
题目 1429: 蓝桥杯2014年第五届真题-兰顿蚂蚁
数据结构·c++·算法·蓝桥杯
苦藤新鸡13 分钟前
35.LRU缓存(最久未访问)问题
算法·链表·缓存
Yupureki15 分钟前
《算法竞赛从入门到国奖》算法基础:入门篇-分治
c语言·开发语言·数据结构·c++·算法·贪心算法
充值修改昵称18 分钟前
数据结构基础:B*树B+树的极致优化
数据结构·b树·python·算法
one____dream20 分钟前
【算法】相同的树与对称二叉树
b树·python·算法·递归
e疗AI产品之路22 分钟前
心电分析诊断算法评估方法介绍
算法·心电分析
爱编码的傅同学22 分钟前
【今日算法】LeetCode 11.盛水最多的容器 15.三数之和 283.移动0
数据结构·算法·leetcode
啊我不会诶22 分钟前
Codeforces Round 1072 (Div. 3)补题
笔记·学习·算法
重生之我是Java开发战士22 分钟前
【算法日记】Set 与 Map 经典算法
算法
alphaTao25 分钟前
LeetCode 每日一题 2026/1/19-2026/1/25
算法·leetcode