상위 150개 인터뷰쉬움

BST 검증

'BST 유효성 검사' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

이진 트리의 루트가 주어지면 이것이 유효한 이진 검색 트리(BST)인지 확인합니다.

유효한 BST는 다음과 같이 정의됩니다.

- 노드의 왼쪽 하위 트리에는 해당 노드의 키보다 엄격하게 작은 키를 가진 노드만 포함됩니다.

- 노드의 오른쪽 하위 트리에는 해당 노드의 키보다 더 큰 키를 가진 노드만 포함됩니다.

- 왼쪽 및 오른쪽 하위 트리 모두 이진 검색 트리여야 합니다.

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

제약
  • The number of nodes in the tree is in the range [1, 10000]
  • -2^31 <= Node.val <= 2^31 - 1

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

The left child 1 < root 2, and right child 3 > root 2. Valid BST.

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

The right child of root is 4, which is less than 5. Also, node 3 is in the right subtree of 5 but is less than 5. Not a valid BST.

Example 3
Input
[5,4,6,None,None,3,7]
Output
False
Explanation

Node 3 is in the right subtree of 5 but has value 3 < 5. Not valid.

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

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