力扣(2024.08.08)

  1. 101:对称二叉树
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 isSymmetric(self, root: Optional[TreeNode]) -> bool:
        def dfs(node, level, res):
            if len(res) == level:
                res.append([])
            if not node:
                res[level].append('None')
            else:
                res[level].append(node.val)
                dfs(node.left, level + 1, res)
                dfs(node.right, level + 1, res)

        res = []
        level = 0
        dfs(root, level, res)
        for i in res:
            if i[::-1] != i:
                return False
        return True
  1. 100:相同的树
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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        def dfs(node, level, res):
            if len(res) == level:
                res.append([])
            if not node:
                res[level].append('None')
            else:
                res[level].append(node.val)
                dfs(node.left, level + 1, res)
                dfs(node.right, level + 1, res)

        res1 = []
        res2 = []
        level = 0
        dfs(p, level, res1)
        dfs(q, level, res2)
        return res1 == res2
  1. 559:N叉树的最大深度
python 复制代码
"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def maxDepth(self, root: 'Node') -> 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 len(res)
  1. 111:二叉树的最小深度
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 minDepth(self, root: Optional[TreeNode]) -> int:
        def dfs(node):
            if not node:
                return 0
            if node.left and not node.right:
                return 1 + dfs(node.left)
            elif not node.left and node.right:
                return 1 + dfs(node.right)
            elif not node.left and not node.right:
                return 1
            elif node.left and node.right:
                return 1 + min(dfs(node.left), dfs(node.right))

        res = dfs(root)
        return res
相关推荐
进击的六角龙19 分钟前
深入浅出:使用Python调用API实现智能天气预报
开发语言·python
檀越剑指大厂19 分钟前
【Python系列】浅析 Python 中的字典更新与应用场景
开发语言·python
VertexGeek20 分钟前
Rust学习(八):异常处理和宏编程:
学习·算法·rust
石小石Orz21 分钟前
Three.js + AI:AI 算法生成 3D 萤火虫飞舞效果~
javascript·人工智能·算法
湫ccc27 分钟前
Python简介以及解释器安装(保姆级教学)
开发语言·python
孤独且没人爱的纸鹤30 分钟前
【深度学习】:从人工神经网络的基础原理到循环神经网络的先进技术,跨越智能算法的关键发展阶段及其未来趋势,探索技术进步与应用挑战
人工智能·python·深度学习·机器学习·ai
羊小猪~~33 分钟前
tensorflow案例7--数据增强与测试集, 训练集, 验证集的构建
人工智能·python·深度学习·机器学习·cnn·tensorflow·neo4j
lzhlizihang35 分钟前
python如何使用spark操作hive
hive·python·spark
q0_0p37 分钟前
牛客小白月赛105 (Python题解) A~E
python·牛客
极客代码40 分钟前
【Python TensorFlow】进阶指南(续篇三)
开发语言·人工智能·python·深度学习·tensorflow