0
0
DSA Javascriptprogramming~20 mins

Validate if Tree is BST in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BST Validation on a Simple Tree
What is the output of the following code that checks if a binary tree is a BST?
DSA Javascript
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(root, min = -Infinity, max = Infinity) {
  if (!root) return true;
  if (root.val <= min || root.val >= max) return false;
  return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

const tree = new Node(10, new Node(5), new Node(15, new Node(6), new Node(20)));
console.log(isBST(tree));
Afalse
BSyntaxError
CTypeError
Dtrue
Attempts:
2 left
💡 Hint
Check if all nodes in the left subtree are less than the root and all nodes in the right subtree are greater.
Predict Output
intermediate
2:00remaining
Result of BST Validation on a Perfect BST
What will be printed after running this code that validates a perfect BST?
DSA Javascript
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(root, min = -Infinity, max = Infinity) {
  if (!root) return true;
  if (root.val <= min || root.val >= max) return false;
  return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

const tree = new Node(8, new Node(3, new Node(1), new Node(6)), new Node(10, null, new Node(14)));
console.log(isBST(tree));
Afalse
Btrue
CReferenceError
Dundefined
Attempts:
2 left
💡 Hint
All left children are less and right children are greater than their parent nodes.
🔧 Debug
advanced
2:00remaining
Identify the Error in BST Validation Code
What error will this code produce when checking if a tree is a BST?
DSA Javascript
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(root, min = -Infinity, max = Infinity) {
  if (!root) return true;
  if (root.val <= min || root.val >= max) return false;
  return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

const tree = new Node(10, new Node(5), new Node(15));
console.log(isBST(tree));
Afalse
BSyntaxError
Ctrue
DLogic error: returns false for valid BST
Attempts:
2 left
💡 Hint
Check the comparison operators in the condition that returns false.
🧠 Conceptual
advanced
2:00remaining
Understanding BST Validation Limits
Why is it necessary to pass min and max values when validating if a tree is a BST?
ATo store the sum of all node values
BTo count the number of nodes in the tree
CTo keep track of the allowed value range for each node during recursion
DTo balance the tree during validation
Attempts:
2 left
💡 Hint
Think about how BST rules apply to all descendants, not just immediate children.
🚀 Application
expert
3:00remaining
Output of BST Validation on a Large Tree with Duplicate Values
Given the following tree with duplicate values, what will the BST validation function return?
DSA Javascript
class Node {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isBST(root, min = -Infinity, max = Infinity) {
  if (!root) return true;
  if (root.val <= min || root.val >= max) return false;
  return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}

const tree = new Node(10,
  new Node(5,
    new Node(3),
    new Node(7, new Node(7), null)
  ),
  new Node(15, null, new Node(20))
);
console.log(isBST(tree));
Afalse
Btrue
CRangeError
DTypeError
Attempts:
2 left
💡 Hint
BSTs do not allow duplicate values on the same side; check the duplicate 7 in the left subtree.