day14-二叉树part01(递归法/迭代法)

递归三部曲(时刻牢记)

1.确认递归函数需要的参数与返回值

一般为传入一个根节点 传入一个数组用来存放结果数组

2.确定终止条件

3.确定单层递归逻辑

递归的实现就是:每一次递归调用都会把函数的局部变量、参数值和返回地址等压入调用栈中,然后递归返回的时候,从栈顶弹出上一次递归的各项参数,所以这就是递归为什么可以返回上一层位置的原因。

迭代用栈完成

二叉树的定义

java 复制代码
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

144. 二叉树的前序遍历 (根左右)

递归

java 复制代码
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        preorder(root,result);
        return result;
    }

    public void preorder(TreeNode root,List<Integer> result){
        if(root == null){
            return;
        }
        result.add(root.val);
        preorder(root.left,result);
        preorder(root.right,result);
    }
}

迭代( 迭代用栈 前序遍历 中左右 进栈顺序 中右左)

java 复制代码
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            result.add(node.val);
            if(node.right != null){
                stack.push(node.right);
            }
            if(node.left != null){
                stack.push(node.left);
            }
        }
        return result;
    }
}

145. 二叉树的后序遍历 (左右根)

递归

java 复制代码
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        postorder(root,result);
        return result;
    }
    private void postorder(TreeNode root,List<Integer> result){
        if(root == null){
            return;
        }
        postorder(root.left,result);
        postorder(root.right,result);
        result.add(root.val);
    }
}

迭代

前序遍历根左右 总是先出根结果 进栈顺序 根 右 左 出栈 根 左 右

后序遍历左右根 进栈顺序 根 左 右 出栈 根 右 左 再反转结果集 左右根

java 复制代码
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            if(node.left != null){
                stack.push(node.left);
            }
            if(node.right != null){
                stack.push(node.right);
            }
            result.add(node.val);
        }
        Collections.reverse(result);
        return result;
    }
}

94. 二叉树的中序遍历 (左根右)

递归

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        inorder(root,result);
        return result;
    }

    public void inorder(TreeNode root,List<Integer> result){
        if(root == null){
            return;
        }
        inorder(root.left,result);
        result.add(root.val);
        inorder(root.right,result);
    }
}

迭代( 中序遍历 左 根 右 一直遍历到最左子节点 弹出 每次弹出先添加右节点)

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            if(cur != null){
                stack.push(cur);
                cur = cur.left;
            }else{
                TreeNode node = stack.pop();
                result.add(node.val);
                cur = node.right;
            }
        }
        return result;
    }
}
相关推荐
爪哇部落算法小助手41 分钟前
每日两题day50
数据结构·c++·算法
curry____3031 小时前
基本算法(2025.11.21)
c++·算法
WWZZ20252 小时前
快速上手大模型:深度学习5(实践:过、欠拟合)
人工智能·深度学习·神经网络·算法·机器人·大模型·具身智能
司铭鸿2 小时前
图论中的协同寻径:如何找到最小带权子图实现双源共达?
linux·前端·数据结构·数据库·算法·图论
小年糕是糕手4 小时前
【C++】C++入门 -- 输入&输出、缺省参数
c语言·开发语言·数据结构·c++·算法·leetcode·排序算法
情怀姑娘4 小时前
面试题---------------场景+算法
java·算法·mybatis
chbmvdd5 小时前
week5题解
数据结构·c++·算法
用户12039112947265 小时前
面试官最爱问的字符串反转:7种JavaScript实现方法详解
算法·面试
vir025 小时前
小齐的技能团队(dp)
数据结构·c++·算法·图论
Star在努力5 小时前
C语言复习八(2025.11.18)
c语言·算法·排序算法