面试算法-169-二叉树的中序遍历

题目

给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。

示例 1:

输入:root = [1,null,2,3]

输出:[1,3,2]

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

    public void dfs(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }

        dfs(root.left, result);
        result.add(root.val);
        dfs(root.right, result);
    }
}
相关推荐
无限进步_1 小时前
【C++】巧用静态变量与构造函数:一种非常规的求和实现
开发语言·c++·git·算法·leetcode·github·visual studio
小超超爱学习99372 小时前
大数乘法,超级简单模板
开发语言·c++·算法
Ricardo-Yang2 小时前
SCNP语义分割边缘logits策略
数据结构·人工智能·python·深度学习·算法
凌波粒2 小时前
LeetCode--344.反转字符串(字符串/双指针法)
算法·leetcode·职场和发展
啊哦呃咦唔鱼2 小时前
LeetCode hot100-543 二叉树的直径
算法·leetcode·职场和发展
秋风不问归客2 小时前
Springboot面试全面整理
spring boot·后端·面试
小冷coding2 小时前
【面试】结合项目整理的场景面试题,覆盖 Java 基础、锁、多线程、数据库、分布式锁 / 事务、消息中间件等核心维度
java·数据库·面试
我叫黑大帅3 小时前
PHP中的官方操作数据库PDO
后端·面试·php
sinat_286945193 小时前
harness engineering
人工智能·算法·chatgpt
少许极端3 小时前
算法奇妙屋(四十三)-贪心算法学习之路10
学习·算法·贪心算法