【leetcode100】二叉树的中序遍历

1、题目描述

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

示例 1:

复制代码
输入:root = [1,null,2,3]
输出:[1,3,2]

2、初始思路

2.1 思路

中序遍历的顺序是左→根→右,定义一个函数进行遍历

复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        result = []
        def traversal(node):
            if node == None:
                return 
            traversal(node.left)
            result.append(node.val)
            traversal(node.right)
        traversal(root)
        return result
相关推荐
2501_916007471 分钟前
Python实现HTTPS爬虫的完整指南:使用requests、BeautifulSoup、Selenium和Scrapy
爬虫·python·ios·小程序·https·uni-app·iphone
gptAI_plus9 分钟前
别把整个仓库塞给 AI:用 Python 生成安全的代码上下文清单
python·chatgpt
吃饱了得干活9 分钟前
Agent 记忆系统:从短期记忆到长期记忆
python·langchain·agent
yyds_yyd_1008611 分钟前
877. 石子游戏(2026.08.02)& 486. 预测赢家(2026.08.01)
c++·leetcode
大鱼>26 分钟前
DSPy:LLM程序自动编译与提示词优化
开发语言·人工智能·python·深度学习
2401_8432537031 分钟前
金融智能:AI如何重构银行业未来
人工智能·python·金融
uncle_ll33 分钟前
服务器选型、微调范式、训练优化与环境搭建
服务器·python·gpt·llm·nlp
久久学姐1 小时前
Python开发爬虫的常用技术架构
爬虫·python·http·框架·数据存储
circuitsosk1 小时前
大规模离线数据管道构建:样本获取、清洗、加工与合成
人工智能·python·机器学习·搜索引擎
tang777891 小时前
分布式爬虫优化指南:如何用代理IP把采集效率提升300%
分布式·爬虫·python·tcp/ip·分布式爬虫·爬虫代理·代理ip