Challenge - 5 Problems
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kth Smallest Element Function Call
What is the output of the following C# code that finds the 3rd smallest element in the given BST?
DSA C#
public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } public class Solution { private int count = 0; private int result = -1; public int KthSmallest(TreeNode root, int k) { InOrder(root, k); return result; } private void InOrder(TreeNode node, int k) { if (node == null) return; InOrder(node.left, k); count++; if (count == k) { result = node.val; return; } InOrder(node.right, k); } } // BST structure: // 5 // / \ // 3 6 // / \ // 2 4 // / // 1 TreeNode root = new TreeNode(5); root.left = new TreeNode(3); root.right = new TreeNode(6); root.left.left = new TreeNode(2); root.left.right = new TreeNode(4); root.left.left.left = new TreeNode(1); Solution sol = new Solution(); int output = sol.KthSmallest(root, 3); System.Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Remember that an inorder traversal of a BST visits nodes in ascending order.
✗ Incorrect
The inorder traversal visits nodes in this order: 1, 2, 3, 4, 5, 6. The 3rd smallest element is 3.
🧠 Conceptual
intermediateUnderstanding Inorder Traversal in BST
Why does an inorder traversal of a Binary Search Tree (BST) return nodes in ascending order?
Attempts:
2 left
💡 Hint
Think about how BST property relates to left and right children values.
✗ Incorrect
In a BST, left children are smaller and right children are larger. Inorder traversal visits left subtree first, then node, then right subtree, so nodes are visited in ascending order.
🔧 Debug
advancedIdentify the Bug in Kth Smallest Element Code
What error will the following code produce when trying to find the 2nd smallest element in the BST?
DSA C#
public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } public class Solution { private int count = 0; private int result = -1; public int KthSmallest(TreeNode root, int k) { InOrder(root, k); return result; } private void InOrder(TreeNode node, int k) { if (node == null) return; InOrder(node.left, k); count++; if (count == k) { result = node.val; } InOrder(node.right, k); } } // BST: 2 nodes only TreeNode root = new TreeNode(1); root.right = new TreeNode(2); Solution sol = new Solution(); int output = sol.KthSmallest(root, 2); System.Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Check if traversal stops after finding kth element.
✗ Incorrect
The code sets result when count == k but does not stop traversal, so it continues visiting nodes unnecessarily. It still prints correct value 2.
❓ Predict Output
advancedOutput of Iterative Kth Smallest Element Code
What is the output of the following iterative C# code to find the 4th smallest element in the BST?
DSA C#
public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } public class Solution { public int KthSmallest(TreeNode root, int k) { Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode current = root; int count = 0; while (current != null || stack.Count > 0) { while (current != null) { stack.Push(current); current = current.left; } current = stack.Pop(); count++; if (count == k) return current.val; current = current.right; } return -1; } } // BST: // 7 // / \ // 3 15 // / \ // 9 20 TreeNode root = new TreeNode(7); root.left = new TreeNode(3); root.right = new TreeNode(15); root.right.left = new TreeNode(9); root.right.right = new TreeNode(20); Solution sol = new Solution(); int output = sol.KthSmallest(root, 4); System.Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Trace the inorder traversal order using the stack.
✗ Incorrect
Inorder traversal order is 3, 7, 9, 15, 20. The 4th smallest element is 15.
🚀 Application
expertNumber of Nodes Visited to Find Kth Smallest
Given a balanced BST with 15 nodes, what is the minimum number of nodes visited during an inorder traversal to find the 7th smallest element using the recursive method that stops traversal after finding the kth element?
Attempts:
2 left
💡 Hint
Consider that inorder traversal visits nodes in ascending order and stops immediately after the kth node.
✗ Incorrect
To find the 7th smallest element, the traversal visits nodes in ascending order until the 7th node. However, due to recursion and tree structure, it may visit one extra node before stopping. So minimum nodes visited is 8.
