力扣(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
相关推荐
用户0099383143011 分钟前
代码随想录算法训练营第十三天 | 二叉树part01
数据结构·算法
shinelord明5 分钟前
【再谈设计模式】享元模式~对象共享的优化妙手
开发语言·数据结构·算法·设计模式·软件工程
游客52010 分钟前
opencv中的各种滤波器简介
图像处理·人工智能·python·opencv·计算机视觉
დ旧言~11 分钟前
专题八:背包问题
算法·leetcode·动态规划·推荐算法
Eric.Lee202112 分钟前
moviepy将图片序列制作成视频并加载字幕 - python 实现
开发语言·python·音视频·moviepy·字幕视频合成·图像制作为视频
Dontla17 分钟前
vscode怎么设置anaconda python解释器(anaconda解释器、vscode解释器)
ide·vscode·python
_WndProc28 分钟前
C++ 日志输出
开发语言·c++·算法
薄荷故人_30 分钟前
从零开始的C++之旅——红黑树及其实现
数据结构·c++
努力学习编程的伍大侠42 分钟前
基础排序算法
数据结构·c++·算法
qq_529025291 小时前
Torch.gather
python·深度学习·机器学习