파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 8. 11. 23:45

https://leetcode.com/problems/validate-binary-search-tree/

 

Validate Binary Search Tree - LeetCode

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

트리 순회 문제.
재귀 호출 활용해서 풀면 된다.

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        def valid_bst(root, min_val, max_val):
            if root is None:
                return True
            if root.val <= min_val or root.val >= max_val:
                return False
            return valid_bst(root.left, min_val, root.val) and valid_bst(root.right, root.val, max_val)
        return valid_bst(root, -2**31-1, 2**31)
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.08.13
Today's Challenge  (0) 2022.08.12
Today's Challenge  (0) 2022.08.10
Today's Challenge  (0) 2022.08.09
Today's Challenge  (0) 2022.08.08
Comments