Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 98 · BST

Validate Binary Search Tree

LeetCode 98 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Pass (min, max) bounds down the tree.

मध्यमTreeDepth-First SearchBinary Search TreeBinary Tree

मी Validate Binary Search Tree (LeetCode 98) असा सोडवला — BST पॅटर्नमधील एक मध्यम प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.

खालील विवरण इंग्रजीत आहे.

Intuition

Every node must lie strictly inside a range set by its ancestors: going left tightens the upper bound to the parent's value, going right tightens the lower bound.

Approach

  1. 1

    validate(node, max, min).

  2. 2

    Fail if node.val ≥ max or ≤ min.

  3. 3

    Left child gets max = node.val; right child gets min = node.val.

मी कसा सोडवला

Range DFSPythonO(n) time · O(h) space
class Solution:
    def isValidBST(self, root):
        def validateTree(node, mx=None, mn=None):
            if mx is not None and node.val >= mx:
                return False
            if mn is not None and node.val <= mn:
                return False
            if node.left and not validateTree(node.left, node.val, mn):
                return False
            if node.right and not validateTree(node.right, mx, node.val):
                return False
            return True
        return validateTree(root)
स्टेप-थ्रू लोड होत आहे…

Validate Binary Search Tree — वारंवार विचारले जाणारे प्रश्न

Validate Binary Search Tree मागील intuition काय आहे?

Every node must lie strictly inside a range set by its ancestors: going left tightens the upper bound to the parent's value, going right tightens the lower bound.

Validate Binary Search Tree (LeetCode 98) कसा सोडवावा?

1. validate(node, max, min). 2. Fail if node.val ≥ max or ≤ min. 3. Left child gets max = node.val; right child gets min = node.val.

Validate Binary Search Tree ची time आणि space complexity किती आहे?

Time: O(n). Space: O(h). (Accepted approach: Range DFS.)

Validate Binary Search Tree मध्ये सामान्य चुका कोणत्या?

Comparing only with the direct parent misses violations deeper down (example 2).