상위 150개 인터뷰하드

이진 트리의 최대 깊이

'이진 트리의 최대 깊이' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

하드

이진 트리의 루트가 주어지면 최대 깊이를 반환합니다.

이진 트리의 최대 깊이는 루트 노드에서 가장 먼 리프 노드까지 가장 긴 경로를 따라 있는 노드 수입니다.

트리는 없음이 누락된 노드를 나타내는 수준 순서 목록으로 표시됩니다. maxDepth(root: list) -> int 함수를 구현하세요.

제약
  • The number of nodes in the tree is in the range [0, 10000]
  • -100 <= Node.val <= 100

Example 1
Input
[3,9,20,None,None,15,7]
Output
3
Explanation

The tree has 3 levels: root [3], second level [9,20], third level [15,7]. The longest path is 3->20->15 or 3->20->7, both of depth 3.

Example 2
Input
[1,None,2]
Output
2
Explanation

The tree has root 1 with only a right child 2. Depth is 2.

Example 3
Input
[]
Output
0
Explanation

An empty tree has depth 0.

Need a Hint?
세트나 힙과 같은 트리 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.