본문 바로가기
LeetCode/Top Interview Q. - Easy

LeetCode. Validate Binary Search Tree [Trees]

by 벤진[Benzene] 2020. 6. 6.

Problem:

Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

-Summary-

1. Create a helper function to run recursively check the left and right subtree.

2. If any subtree does not valid with a BST requirement, it will return False.

3. If all checked without any False, it will return True.

Recursive 하게 풀었지만 풀이를 보니, Iterate하게 푸는방법과 Inorder Traversal 로 푸는 방법도 있었다.

또한 이번 문제를 풀면서, python에서 infinity는 float('inf')로 표현할수 있다는것을 배웠다.

 

leetcode.com/problems/validate-binary-search-tree/discuss/114459/3-python-solutions-clamping-window-in-order-traversal

 

3 python solutions, clamping window, in-order traversal - LeetCode Discuss

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

댓글