[LeetCode] Maximum Depth of Binary Tree

Updated:

문제

Given the root of a binary tree, return its maximum depth.

A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

예제

Example 1:

img

Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:

Input: root = [1,null,2]
Output: 2

Example 3:

Input: root = []
Output: 0

Example 4:

Input: root = [0]
Output: 1

조건

  • The number of nodes in the tree is in the range [0, $10^4$].
  • -100 <= Node.val <= 100

풀이 1

BFS를 활용한 풀이

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

Leave a comment