Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 2.3 KB

File metadata and controls

35 lines (28 loc) · 2.3 KB

Valid Binary Search Tree medium #javascript #blind75 #binary-search-tree #recursion

by Pawan Kumar @jsartisan

Take the Challenge

Given the root of a binary tree, determine if it's a valid binary search tree (BST).

A binary search tree is valid if:

  • All nodes in the left subtree have values less than the current node
  • All nodes in the right subtree have values greater than the current node
  • Both left and right subtrees are also valid BSTs

Constraints:

  • 1 ≤ Number of nodes ≤ 1000
  • -1000 ≤ Node values ≤ 1000

Examples:

// Example 1:
//     2
//    / \
//   1   3
const root1 = createTree([2, 1, 3]);
console.log(isValidBST(root1));
// Output: true

// Example 2:
//     1
//    / \
//   2   3
const root2 = createTree([1, 2, 3]);
console.log(isValidBST(root2));
// Output: false
// Explanation: Left child (2) is greater than root (1)

Back Share your Solutions Check out Solutions