2024.5.6力扣刷题记录-二叉树学习记录5(未完)

一、学习视频

【二叉树的层序遍历【基础算法精讲 13】】 https://www.bilibili.com/video/BV1hG4y1277i/?share_source=copy_web\&vd_source=dc0e55cfae3b304619670a78444fd795

二、跟练代码

1.102. 二叉树的层序遍历

python 复制代码
# 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 levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        # 双列表
        if not root:
            return []
        # 现在节点、子节点、现在节点值列表
        cur, ans = [root], []
        while cur:
            nxt, val = [], []
            # 遍历现在节点列表
            for x in cur:
                val.append(x.val)
                if x.left:
                    nxt.append(x.left)
                if x.right:
                    nxt.append(x.right)
            ans.append(val)
            cur = nxt
        return ans

(未完待续)

相关推荐
tkevinjd7 小时前
力扣300-最长递增子序列
算法·leetcode·职场和发展·动态规划·贪心
用户298698530147 小时前
Python 实现 Excel 到 ODS、XPS、PostScript 及 PDF/A-1b 的格式转换
后端·python·excel
dankokoko7 小时前
7.13-7.14学习
学习
本地化文档7 小时前
xlwings-docs-l10n
python·github·excel·gitcode·sphinx
梅雅达编程笔记8 小时前
零基础学 Python 第7章 | 字典 dict:键值对存储
开发语言·python·beautifulsoup·numpy·pandas
heroboyluck8 小时前
AI工程师第四课 - 深度学习入门
人工智能·python·深度学习·llama
一楼的猫8 小时前
网文AI辅助怎么过审?番茄200+维度检测与去AI味实战
人工智能·学习·机器学习·自然语言处理·ai写作
小李飞刀李寻欢8 小时前
DeepSeek V3 版本模型结构分析
算法·大模型·deepseek
alicema11118 小时前
BRAIN 研究顾问(Research Consultant)兼职
python·量化