0
0
DSA Javascriptprogramming~20 mins

Kth Smallest Element in BST in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Kth Smallest Element Function Call
What is the output of the following JavaScript code that finds the 3rd smallest element in a BST?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function kthSmallest(root, k) {
  let count = 0;
  let result = null;

  function inorder(node) {
    if (!node || result !== null) return;
    inorder(node.left);
    count++;
    if (count === k) {
      result = node.val;
      return;
    }
    inorder(node.right);
  }

  inorder(root);
  return result;
}

const root = new TreeNode(5,
  new TreeNode(3,
    new TreeNode(2,
      new TreeNode(1), null),
    new TreeNode(4)),
  new TreeNode(6)
);

console.log(kthSmallest(root, 3));
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Remember that inorder traversal of BST gives sorted order.
🧠 Conceptual
intermediate
1:30remaining
Understanding Inorder Traversal in BST
Why does inorder traversal help find the kth smallest element in a BST?
ABecause inorder traversal visits only leaf nodes.
BBecause inorder traversal visits nodes in descending order of their values.
CBecause inorder traversal visits nodes randomly.
DBecause inorder traversal visits nodes in ascending order of their values.
Attempts:
2 left
💡 Hint
Think about the BST property and how inorder traversal works.
🔧 Debug
advanced
2:00remaining
Identify the Error in Kth Smallest Implementation
What error will occur when running this code to find the kth smallest element?
DSA Javascript
function kthSmallest(root, k) {
  let count = 0;
  let result = null;

  function inorder(node) {
    if (!node) return;
    inorder(node.left);
    if (count === k) {
      result = node.val;
      return;
    }
    count++;
    inorder(node.right);
  }

  inorder(root);
  return result;
}
AIt returns null for any k because count is incremented after checking equality.
BIt throws a ReferenceError because result is not defined.
CIt returns the correct kth smallest element.
DIt causes infinite recursion.
Attempts:
2 left
💡 Hint
Check the order of count increment and comparison with k.
Predict Output
advanced
2:00remaining
Output of Kth Smallest with Duplicate Values
What is the output of the code below when finding the 4th smallest element in a BST with duplicates?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function kthSmallest(root, k) {
  let count = 0;
  let result = null;

  function inorder(node) {
    if (!node || result !== null) return;
    inorder(node.left);
    count++;
    if (count === k) {
      result = node.val;
      return;
    }
    inorder(node.right);
  }

  inorder(root);
  return result;
}

const root = new TreeNode(5,
  new TreeNode(3,
    new TreeNode(3,
      new TreeNode(2), null),
    new TreeNode(4)),
  new TreeNode(6)
);

console.log(kthSmallest(root, 4));
A4
B5
C3
D2
Attempts:
2 left
💡 Hint
Duplicates appear in sorted order during inorder traversal.
🚀 Application
expert
2:30remaining
Optimizing Kth Smallest Element Search
Which data structure modification allows finding the kth smallest element in a BST in O(log n) time on average?
AUse a stack to perform iterative inorder traversal each time.
BAugment each node with the count of nodes in its left subtree.
CStore all elements in an array and sort it every time.
DConvert the BST into a linked list.
Attempts:
2 left
💡 Hint
Think about how to quickly skip nodes during traversal.