Skip to content
RED SAGSkills
medium10 pts · first answer countsgraphsbinary-search-treedebugging

Bug in a parent-child BST check

This function checks only that each left child is smaller and each right child is larger than its parent. Why is it wrong?

function isBST(n) {
  if (!n) return true
  if (n.left && n.left.val >= n.val) return false
  if (n.right && n.right.val <= n.val) return false
  return isBST(n.left) && isBST(n.right)
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions