DAY16 代码随想录 二叉树part03

104.二叉树的最大深度

层次遍历-BFS思路

# 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
        deep = 1
        queue = [(root,deep)]
        while queue:
            node,deep = queue.pop(0)
            if node.left:
                queue.append((node.left,deep+1))
            if node.right:
                queue.append((node.right,deep+1))
        return deep

递归-DFS思路

# 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

        leftHeight = self.maxDepth(root.left)
        rightHeight = self.maxDepth(root.right)
        return max(leftHeight,rightHeight)+1
        

559.n叉树的最大深度

"""
# 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:
        # 若当前节点为空则deep为0
        if not root: return 0
        # 若当前节点没有child 则deep =1
        if not root.children:return 1
        return max(self.maxDepth(node)+1 for node in root.children)
"""
# 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:
        queue = []
        queue.append(root)
        level = 0
        if not root:return 0

        while queue:
            # 该层的节点数量
            queueLen = len(queue)
            level+=1
            for i in range(queueLen):
                node = queue.pop(0)
                # 如果当前节点依然有孩子节点 则把他的孩子节点也加到队列中
                if node.children and len(node.children) > 0:
                    for item in node.children:
                        queue.append(item)
        return level

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
        if not root.left or not root.right:return 1
        leftHeight = self.minDepth(root.left)
        rightHeight = self.minDepth(root.right)
        return min(leftHeight,rightHeight)+1
# 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
        # 只存在根节点
        if not root.left and not root.right:return 1
        # 定义左子树最小值和右子树最小值
        leftMin = self.minDepth(root.left)
        rightMin = self.minDepth(root.right)
        # 左子树不为空且右子树为空
        if root.left and not root.right:
            return leftMin+1
        # 左子树为空且右子树不为空
        if root.right and not root.left:
            return rightMin+1
        # 左右子树都不为空
        return min(leftMin,rightMin)+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
        sum = 0
        queue = [root]
        while queue:
            node = queue.pop(0)
            sum+=1
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
        return sum

递归

# 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
        # 求左右子树深度
        leftdep = self.deep(root.left)
        rightdep = self.deep(root.right)
        # 若左子树深度等于右子树则说明左子树为满二叉树
        if leftdep == rightdep:
            return (2** leftdep -1) +self.countNodes(root.right)+1
        # 若左子树深度大于右子树则说明右子树为满二叉树
        else:
            return (2**rightdep-1)+self.countNodes(root.left)+1

    """求二叉树的深度-直接用完全二叉树性质求,一直搜索其左子树"""
    def deep(self,root):
        deep = 0
        while root:
            root = root.left
            deep+=1
        return deep
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值