상위 150개 인터뷰중간

균형 잡힌 이진 트리

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

문제 설명

중간

이진 트리가 주어지면 높이 균형이 맞는지 확인합니다.

높이 균형 이진 트리는 모든 노드의 두 하위 트리 깊이가 1 이상 다르지 않은 이진 트리입니다.

트리는 레벨 순서 목록으로 표시됩니다. isBalanced(root: list) -> bool 함수를 구현하세요.

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

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

The left subtree (rooted at 9) has depth 1, and the right subtree (rooted at 20) has depth 2. The difference is 1, so the tree is balanced.

Example 2
Input
[1,2,2,3,3,None,None,4,4]
Output
False
Explanation

The left subtree has depth 3 while the right subtree has depth 1. The difference is 2, so the tree is not balanced.

Example 3
Input
[]
Output
True
Explanation

An empty tree is considered balanced.

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 리소스

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