0
点赞
收藏
分享

微信扫一扫

【.NET AI Books】问题分类和技能使用大全

做个橙梦 2023-06-15 阅读 47
算法

104. 二叉树的最大深度

# 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:
        if not root:
            return 0
        left = self.maxDepth(root.left)
        right = self.maxDepth(root.right)
        return max(left,right) + 1

111. 二叉树的最小深度

# 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:
        if not root:
            return 0
        left = self.minDepth(root.left)
        right = self.minDepth(root.right)
        if not root.left and root.right:
            return 1+right
        if not root.right and root.left:
            return 1+left
        return min(left,right)+1

 要注意:叶子节点是指没有左右子节点的节点。那么就不能无脑return min(left,right)+1 因为如果左边是空右边不是空那么会直接返回1而不是右边的长度

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

暴力解法

# 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 countNodes(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        res = []
        queue = deque()
        queue.append(root)
        while queue:
            size = len(queue)
            for i in range(0, size):
                node = queue.popleft()
                res.append(node)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return len(res)

普通二叉树写法 递归 卧槽比循环还慢!

# 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 countNodes(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        left = self.countNodes(root.left)
        right = self.countNodes(root.right)
        return 1+left+right

完全二叉树写法 递归 更tm慢了

# 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 countNodes(self, root: Optional[TreeNode]) -> int:
        if not root: 
            return 0
        count = 1
        left = root.left
        right = root.right
        while left and right:
            count +=1
            left = left.left
            right = right.right
        if not left and not right: # 如果同时到底说明是满二叉树
            return 2**count-1
        return 1+self.countNodes(root.left)+self.countNodes(root.right)

举报

相关推荐

0 条评论