링크
올바른 bst 인지 판단하는 문제 (left.val < root.val < right.val)
def isValidBST(self, root: Optional[TreeNode]) -> bool:
return self.isValidBSTHelper(root)
def isValidBSTHelper(self, root) ->bool:
left = right = []
if root.left:
left = self.isValidBSTHelper(root.left)
if not left or (left and root.val <= left[-1]): return []
if root.right:
right = self.isValidBSTHelper(root.right)
if not right or (right and root.val >= right[0]): return []
return left + [root.val] + right
def isValidBST(self, root):
output = []
self.inOrder(root, output)
for i in range(1, len(output)):
if output[i-1] >= output[i]:
return False
return True
def inOrder(self, root, output):
if root is None:
return
self.inOrder(root.left, output)
output.append(root.val)
self.inOrder(root.right, output)