Learn by doing, not just watching. Solve real-world challenges in our interactive coding environment with instant feedback and expert guidance.
Write a function to determine if a binary tree is a valid binary search tree. A valid BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.
Learning Objectives
Tree traversal, recursion, data structure validation
Time Estimate
30-45 minutes
Completed By
2,847 students
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
function isValidBST(root) {
// Your code here
return true;
}
// Test cases
const tree1 = new TreeNode(2);
tree1.left = new TreeNode(1);
tree1.right = new TreeNode(3);
console.log(isValidBST(tree1)); // true
Choose your learning path and start solving challenges tailored to your goals
Master arrays, linked lists, trees, graphs, and hash tables
Practice sorting, searching, dynamic programming, and more
Build real-world projects with HTML, CSS, and JavaScript
Query optimization, schema design, and data modeling
Scalability, architecture patterns, and distributed systems
Implement ML algorithms and work with real datasets
Our browser-based IDE supports 15+ programming languages with instant feedback
1 function twoSum(nums, target) {
2 const map = new Map();
3
4 for (let i = 0; i < nums.length; i++) {
5 const complement = target - nums[i];
6
7 if (map.has(complement)) {
8 return [map.get(complement), i];
9 }
10
11 map.set(nums[i], i);
12 }
13
14 return [];
15 }
16
17 // Test cases
18 console.log(twoSum([2, 7, 11, 15], 9));
Test Case 1: Passed
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Expected: [0,1]
Test Case 2: Passed
Input: nums = [3,2,4], target = 6
Output: [1,2]
Expected: [1,2]
Test Case 3: Passed
Input: nums = [3,3], target = 6
Output: [0,1]
Expected: [0,1]
✓ All test cases passed!
Runtime: 68ms | Memory: 42.3MB
Most solved challenges this week
Write a function that reverses a string. The input string is given as an array of characters.
Given a string, find the length of the longest substring without repeating characters.
Given two sorted arrays, find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
See what our community is building and get inspired for your next project
Alex Martinez
Full Stack Developer
Interactive dashboard built with React and D3.js for visualizing real-time data streams
Sarah Chen
Frontend Developer
Full-featured online store with cart, checkout, and payment integration using Next.js
Marcus Johnson
Mobile Developer
Cross-platform mobile app for workout tracking and health monitoring with React Native
Join thousands of developers improving their skills every day. Start solving challenges and building your portfolio today.