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 회사에 있습니다. [Copyright © 2020 LeetCode]
'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글
LeetCode. Binary Tree Level Order Traversal [Trees] (0) | 2020.06.07 |
---|---|
LeetCode. Symmetric Tree [Trees] (0) | 2020.06.07 |
LeetCode. Maximum Depth of Binary Tree [Trees] (0) | 2020.06.05 |
LeetCode. Linked List Cycle [Linked List] (0) | 2020.06.04 |
LeetCode. Palindrome Linked List [Linked List] (0) | 2020.06.04 |
댓글