0
0
DSA Typescriptprogramming~20 mins

Validate if Tree is BST in DSA Typescript - 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 Function
What is the output of the following TypeScript function when called with the given tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function isValidBST(root: TreeNode | null, min: number = -Infinity, max: number = Infinity): boolean {
  if (root === null) return true;
  if (root.val <= min || root.val >= max) return false;
  return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max);
}

// Tree structure:
//      5
//     / \
//    3   7
//   / \   \
//  2   4   8

const tree = new TreeNode(5,
  new TreeNode(3, new TreeNode(2), new TreeNode(4)),
  new TreeNode(7, null, new TreeNode(8))
);

console.log(isValidBST(tree));
Atrue
Bfalse
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
Check if all nodes follow the BST property: left < root < right.
Predict Output
intermediate
2:00remaining
Output for Invalid BST Tree
What is the output of the isValidBST function for the following tree?
DSA Typescript
// Tree structure:
//      5
//     / \
//    3   7
//   / \   \
//  2   6   8

const invalidTree = new TreeNode(5,
  new TreeNode(3, new TreeNode(2), new TreeNode(6)),
  new TreeNode(7, null, new TreeNode(8))
);

console.log(isValidBST(invalidTree));
Afalse
Btrue
CReferenceError
DRangeError
Attempts:
2 left
💡 Hint
Check if the left subtree contains values greater than root.
🔧 Debug
advanced
2:00remaining
Identify the Error in BST Validation Code
What error will the following code produce when run?
DSA Typescript
function isValidBST(root: TreeNode | null): boolean {
  if (!root) return true;
  if (root.left && root.left.val >= root.val) return false;
  if (root.right && root.right.val <= root.val) return false;
  return isValidBST(root.left) && isValidBST(root.right);
}

// Tree:
//      10
//     /  \
//    5    15
//        /  \
//       6    20

const tree = new TreeNode(10,
  new TreeNode(5),
  new TreeNode(15, new TreeNode(6), new TreeNode(20))
);

console.log(isValidBST(tree));
Afalse
BNo error, outputs true
CStackOverflowError
Dtrue
Attempts:
2 left
💡 Hint
Check if the function correctly validates all BST rules beyond immediate children.
🧠 Conceptual
advanced
1:30remaining
Why Use Min and Max in BST Validation?
Why does the isValidBST function use min and max parameters during recursion?
ATo store the sum of all node values in the tree.
BTo count the number of nodes visited during traversal.
CTo keep track of the allowed value range for each node to ensure BST property is maintained.
DTo balance the tree by adjusting node values.
Attempts:
2 left
💡 Hint
Think about how BST property restricts node values relative to ancestors.
🚀 Application
expert
3:00remaining
Count Valid BST Subtrees in a Binary Tree
Given a binary tree, how many subtrees are valid BSTs? Consider each node as root of a subtree. Use the isValidBST function to check each subtree.
DSA Typescript
function countBSTSubtrees(root: TreeNode | null): number {
  if (!root) return 0;
  let count = 0;
  if (isValidBST(root)) count++;
  count += countBSTSubtrees(root.left);
  count += countBSTSubtrees(root.right);
  return count;
}

// Tree:
//      10
//     /  \
//    5    15
//        /  \
//       6    20

const tree = new TreeNode(10,
  new TreeNode(5),
  new TreeNode(15, new TreeNode(6), new TreeNode(20))
);

console.log(countBSTSubtrees(tree));
A3
B2
C5
D4
Attempts:
2 left
💡 Hint
Check each subtree rooted at every node and count how many are valid BSTs.