Depth-First Search pattern
Validate Binary Search Tree
Given the root of a binary tree, determine whether it is a valid binary search tree, where every node in a left subtree is strictly less than its parent and every node in a right subtree is strictly greater. Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 98
- Medium
- 2 approaches
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
Depth-First SearchLeetCode 98
Medium
From the first idea to the best one
- 01 Brute force (each node vs subtree min/max) O(n²)O(h) space
- 02 Optimized DFS (carry low/high bounds down) O(n)O(h) space
What you will learn
- How to read Validate Binary Search Tree and spot the depth-first search pattern in it
- Brute force (each node vs subtree min/max): O(n²) time, O(h) space
- Optimized DFS (carry low/high bounds down): O(n) time, O(h) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Depth-First Search
See Validate Binary Search Tree run, step by step
Sign in and this page opens on the animation, with all 18 Depth-First Search problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.