104.二叉树的最大深度

2023.6.12
层序遍历记录层数即可

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if not root:
            return 0
        
        depth = 0
        queue = collections.deque([root])
        
        while queue:
            depth += 1
            for _ in range(len(queue)):
                node = queue.popleft()
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        
        return depth

你可能感兴趣的:(Notes,of,Leetcode,leetcode,算法,职场和发展)