【leetcode】94. 二叉树的中序遍历

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

示例 1:

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

输出:[1,3,2]

示例 2:

输入:root = []

输出:[]

示例 3:

输入:root = [1]

输出:[1]

python 复制代码
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def inorderTraversal(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: List[int]
        """
        res = []
        def dfs(node):
            if node is None:
                return
            
            dfs(node.left)
            res.append(node.val)
            dfs(node.right)
        
        dfs(root)
        return res  
相关推荐
Django强哥12 分钟前
JSON Schema Draft-07 详细解析
javascript·算法·代码规范
AndrewHZ12 分钟前
【图像处理基石】GIS图像处理入门:4个核心算法与Python实现(附完整代码)
图像处理·python·算法·计算机视觉·gis·cv·地理信息系统
杨小码不BUG41 分钟前
蛇形舞动:矩阵填充的艺术与算法(洛谷P5731)
c++·算法·矩阵·csp-j/s·循环控制
MicroTech20251 小时前
微算法科技(NASDAQ:MLGO)开发延迟和隐私感知卷积神经网络分布式推理,助力可靠人工智能系统技术
人工智能·科技·算法
Boop_wu2 小时前
[数据结构] Map和Set
java·数据结构·算法
思考的笛卡尔3 小时前
密码学基础:RSA与AES算法的实现与对比
网络·算法·密码学
格林威9 小时前
常规线扫描镜头有哪些类型?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
程序员莫小特11 小时前
老题新解|大整数加法
数据结构·c++·算法
过往入尘土12 小时前
服务端与客户端的简单链接
人工智能·python·算法·pycharm·大模型
zycoder.12 小时前
力扣面试经典150题day1第一题(lc88),第二题(lc27)
算法·leetcode·面试