力扣(2024.08.07)

  1. 637:二叉树的层平均值
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 averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
        def dfs(node, level, res):
            if not node:
                return
            if len(res) == level:
                res.append([])
            res[level].append(node.val)
            dfs(node.left, level + 1, res)
            dfs(node.right, level + 1, res)

        res = []
        level = 0
        dfs(root, level, res)

        final_res = []
        for i in res:
            final_res.append(sum(i) / len(i))
        return final_res
  1. 429:N叉树的层序遍历
python 复制代码
"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        def dfs(node, level, res):
            if not node:
                return
            if len(res) == level:
                res.append([])
            res[level].append(node.val)
            for child in node.children:
                dfs(child, level + 1, res)

        res = []
        level = 0
        dfs(root, level, res)
        return res       
  1. 515:在每个树行中找最大值
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 largestValues(self, root: Optional[TreeNode]) -> List[int]:
        def dfs(node, level, res):
            if not node:
                return
            if len(res) == level:
                res.append([])
            res[level].append(node.val)
            dfs(node.left, level + 1, res)
            dfs(node.right, level + 1, res)

        res = []
        level = 0
        dfs(root, level, res)

        final_res = []
        for i in res:
            final_res.append(max(i))
        return final_res
  1. 104:二叉树的最大深度
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 maxDepth(self, root: Optional[TreeNode]) -> int:
        def dfs(node, level, res):
            if not node:
                return
            if len(res) == level:
                res.append([])
            res[level].append(node.val)
            dfs(node.left, level + 1, res)
            dfs(node.right, level + 1, res)

        res = []
        level = 0
        dfs(root, level, res)
        return len(res)
  1. 226:翻转二叉树
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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        def dfs(node):
            if not node:
                return
            node.left, node.right = node.right, node.left
            dfs(node.left)
            dfs(node.right)

        dfs(root)
        return root
相关推荐
用户277844910499310 小时前
借助DeepSeek智能生成测试用例:从提示词到Excel表格的全流程实践
人工智能·python
算AI12 小时前
人工智能+牙科:临床应用中的几个问题
人工智能·算法
JavaEdge在掘金12 小时前
ssl.SSLCertVerificationError报错解决方案
python
我不会编程55512 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
老歌老听老掉牙13 小时前
平面旋转与交线投影夹角计算
python·线性代数·平面·sympy
满怀101513 小时前
Python入门(7):模块
python
无名之逆13 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
你觉得20513 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义下载方法
大数据·人工智能·python·gpt·学习·机器学习·aigc
啊喜拔牙13 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala
owde13 小时前
顺序容器 -list双向链表
数据结构·c++·链表·list