代码随想录 - Day23 - 二叉树最大深度&最小深度

代码随想录 - Day23 - 二叉树最大深度&最小深度

104. 二叉树的最大深度

  • 递归法
    • 使用前序遍历(找深度)或后序遍历(找高度)
  • 迭代法
    • 使用层序遍历
# 递归 前序

# 递归 后序
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        return self.getDepth(root)
        
    def getDepth(self, node):
        if not node:
            return 0
        leftHeight = self.getDepth(node.left)
        rightHeight = self.getDepth(node.right)
        hight = 1 + max(leftHeight, rightHeight)
        return hight
# 迭代 层序
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        queue = collections.deque([root])
        cnt = 0
        while queue:
            size = len(queue)
            for _ in range(size):
                cur = queue.popleft()
                if cur.left:
                    queue.append(cur.left)
                if cur.right:
                    queue.append(cur.right)
            cnt += 1
        return cnt

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:
        if not root:
            return 0
        depth = 1
        for child in root.children:
            depth = max(depth, self.maxDepth(child) + 1)
        return depth
  • 迭代法:层序遍历
class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root:
            return 0
        queue = collections.deque([root])
        depth = 0
        while queue:
            depth += 1
            size = len(queue)
            for _ in range(len(queue)):
                cur = queue.popleft()
                for child in cur.children:
                    queue.append(child)
        return depth

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:
        return self.getDepth(root)
        
    def getDepth(self, node):
        if not node:
            return 0
        leftDepth = self.getDepth(node.left)
        rightDepth = self.getDepth(node.right)

        # 要注意根节点只有一侧节点的情况并不是深度为1
        if not node.left and node.right:
            return 1 + rightDepth
        if node.left and not node.right:
            return 1 + leftDepth
        return 1 + min(leftDepth, rightDepth)
  • 迭代法:层序遍历,除了根节点,一旦遇到一个没有左节点或右节点的节点,就返回累计的深度
class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        queue = collections.deque([root])
        cnt = 0
        while queue:
            size = len(queue)
            cnt += 1
            for _ in range(size):
                cur = queue.popleft()
                if not cur.left and not cur.right:
                    return cnt
                if cur.left:
                    queue.append(cur.left)
                if cur.right:
                    queue.append(cur.right)
        return cnt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值