상위 150개 인터뷰중간

이진 트리 반전

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

문제 설명

중간

이진 트리의 루트가 주어지면 트리를 반전하고 루트를 반환합니다.

이진 트리를 뒤집는다는 것은 트리에 있는 모든 노드의 왼쪽 및 오른쪽 자식을 바꾸는 것을 의미합니다.

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

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

Example 1
Input
[4,2,7,1,3,6,9]
Output
[4,7,2,9,6,3,1]
Explanation

The root stays 4. Its children swap: left becomes 7, right becomes 2. Their children also swap recursively.

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

The root stays 2. Left child 1 and right child 3 are swapped.

Example 3
Input
[]
Output
[]
Explanation

An empty tree inverted is still empty.

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

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