↓Skip to main content
  1. LeetCode/

LeetCode 98: Validate Binary Search Tree

·2 mins· ·
LeetCode Medium Tree Depth-First-Search Binary-Search-Tree Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-29

  • Submissions: Runtime Error ×2 → Wrong Answer ×2 → Runtime Error → Wrong Answer ×2 → Accepted → Wrong Answer → Runtime Error ×3 → Wrong Answer ×3 → Accepted

Approach
#

A BST needs the entire left subtree below the node and the entire right subtree above it, not just the children (Tree Template 3). Pass a range (lo, hi) down:

  • The node’s value must be strictly inside it.
  • Going left the upper bound becomes the node’s value; going right the lower bound does.

My Attempts
#

This took 16 submissions, mostly from comparing only parents with children: in [15, None, 22, 12], 22 exceeds 15 and 12 is below 22, so every parent-child pair is fine; but 12 sits in 15’s right subtree while being smaller than 15, so the answer is False; it returned True. Other errors: equal values were allowed ([9, 9] returned True), a node with one child returned True immediately, and one returned a tuple.

Solution
#

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        def ok(node, lo, hi):
            if not node:
                return True
            if not lo < node.val < hi:
                return False
            return ok(node.left, lo, node.val) and ok(node.right, node.val, hi)
        return ok(root, float("-inf"), float("inf"))

Complexity
#

Time O(n), space O(h). An in-order traversal checking strict increase also works.

Mistakes to Avoid
#

  • Comparing a node with its children is not enough; a grandchild may break an ancestor’s range.
  • Strict inequalities: equal values are invalid.

Interview Explanation
#

A BST constrains whole subtrees, so I pass a valid range down, tightening the upper bound going left and the lower bound going right, and require every node strictly inside. Time O(n).

Related

LeetCode 230: Kth Smallest Element in a BST
·1 min
LeetCode Medium Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 230 note: a BST’s in-order traversal is ascending, so stop an iterative in-order walk at the k-th node
LeetCode 235: Lowest Common Ancestor of a Binary Search Tree
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 235 note: go left when both p and q are smaller, right when both are larger; otherwise (split, or one is the node itself) this node is the LCA
LeetCode 1448: Count Good Nodes in Binary Tree
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 note: pass the path maximum down during DFS; a node is good when its value is at least that maximum
LeetCode 199: Binary Tree Right Side View
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 199 note: in a level-order traversal, each level’s rightmost node is what you see from the right
LeetCode 938: Range Sum of BST
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 938 note: prune with the BST order, going only right below low and only left above high, adding nodes inside the range
LeetCode 606: Construct String from Binary Tree
·1 min
LeetCode Medium String Tree Depth-First-Search Binary-Tree
LeetCode 606 note: output in pre-order, keeping an empty () when only the left subtree is missing, and omitting the right parentheses when it is empty