算法训练第十五天

110.平衡二叉树

代码:

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 isBalanced(self, root: Optional[TreeNode]) -> bool:
        ans = [True]
        a = self.find(root,ans)
        return ans[0]

    def find(self,node,ans):
        if node is None:
            return 0
        left = self.find(node.left,ans)
        right = self.find(node.right,ans)
        if abs(left-right)>1:
            ans[0] = False
        return max(left,right)+1

257.二叉树的所有路径

代码:

python 复制代码
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    import copy
    def binaryTreePaths(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: List[str]
        """
        ans = []
        res = []
        self.find(root,res,ans)
        return ans
    
    def find(self,node,res,ans):
        res.append(str(node.val))
        if node.left is None and node.right is None:
            ans.append(copy.deepcopy('->'.join(res)))
            
            res.pop()
            return
        if node.left:
            self.find(node.left,res,ans)
        if node.right:
            self.find(node.right,res,ans)
        res.pop()

        

404.左叶子之和

代码:

python 复制代码
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def sumOfLeftLeaves(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: int
        """
        ans = []
        lf=False
        self.find(root,ans,lf)
        return sum(ans)

    def find(self,node,ans,lf):
        if node.left is None and node.right is None:
            if lf:
                ans.append(node.val)
        if node.left:
            self.find(node.left,ans,True)
        if node.right:
            self.find(node.right,ans,False)

222.完全二叉树的节点个数

代码:

python 复制代码
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def countNodes(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: int
        """
        ans = []
        self.find(root,ans)
        return len(ans)

    def find(self,node,ans):
        if node:
            ans.append(node.val)
            self.find(node.left,ans)
            self.find(node.right,ans)
相关推荐
Everybody_up1 天前
大模型开发中format_messages、invoke、format三种方法的对比
人工智能·python·大模型
冉卓电子1 天前
MPC5604B/C MC_RGM 复位模块全解
c语言·开发语言·单片机·嵌入式硬件
鱼子星_1 天前
【数据结构与算法】数据结构基础——树(上):树的存储结构,满二叉树,完全二叉树,二叉树的存储结构
c语言·数据结构·算法
Chris _data1 天前
C# 与 PLC Modbus RTU 通信实践:从单例到线程安全的连接监控
开发语言·安全·c#
不负岁月无痕1 天前
STL-- C++ vector类 模拟实现
开发语言·c++
晚烛1 天前
CANN 分布式通信与 HCCL:多 NPU 协作的底层机制
开发语言·人工智能·分布式·python·深度学习
装不满的克莱因瓶1 天前
新版AI开发框架SpringAIAlibaba vs AgentScope 选型指南
java·开发语言·人工智能·ai·agent·alibaba·springai
雾酩1 天前
深拷贝与浅拷贝:一篇彻底讲明白的入门博客
开发语言·前端·javascript
丘山望岳1 天前
C++模板特化:类型与常量的灵活掌控
c语言·开发语言·c++
阿里嘎多学长1 天前
2026-05-24 GitHub 热点项目精选
开发语言·程序员·github·代码托管