Kth Smallest Element in a Bst - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We leverage the fact that an in-order traversal of a Binary Search Tree (BST) visits nodes in ascending order. The provided k parameter acts as a mutable downward counter indicating how many more nodes remain before we reach the kth smallest element. Each time we “visit” a node (i.e., process it between its left and right recursive calls), we decrement k. When k reaches 0 immediately after visiting a node, that node is the kth smallest. If a non-null result is returned from the left subtree, it means the kth smallest element was found there, so we return it immediately, allowing the result value to naturally propagate upward through the recursion stack after it is found.

🔧 Traversal Logic

  • Base case: If the current node is null, return null — signaling that no result was found in this branch.
  • Go left first: Recursively traverse the left subtree to enumerate all smaller values (if any).
  • Bubble up early if found on the left: If the left recursion returns a non-null value, it means the kth smallest element was already found there, so we return it immediately — skipping the current node and its right subtree.
  • Visit node: Otherwise, process the current node and decrement k.
  • Result-found condition: When k == 0 after visiting a node, we’ve found the kth smallest element. Return it immediately — the result propagates up the recursion stack.
  • Then go right: If the kth element wasn’t found in the left subtree or the current node, we recurse into the right subtree to continue counting larger elements in ascending order. The recursive call will inevitably return the correct result, so we can unconditionally return its value upward.

🧠 Correctness Intuition

In a BST, left-subtree values are smaller and right-subtree values are larger. Thus, in-order traversal yields a strictly increasing sequence. Counting down k at each visit aligns with the node’s rank in that order — when k hits 0, we’re at the kth smallest. Early return after the left recursion (when it already found the answer) preserves correctness and avoids unnecessary work.

🧭 Result Semantics

When k reaches 0 upon visiting a node, that node’s value is the kth smallest. The function returns it immediately, and the value is bubbled up through recursive returns to the caller.

Time Complexity

Let h be the tree height and n be the tree size.
O(h + k) — at most h recursive calls are active at a time, and k nodes are visited to decrement the counter before finding the result. Worst case O(n) when k = n or the tree is skewed going down left.

Space Complexity

Let h be the tree height and n be the tree size.
O(h) — the recursion stack depth is upper-bounded by the tree’s height (O(log n) for balanced trees, O(n) for skewed ones).

Visucodize editorial solution titled JS Solution for LeetCode's Kth Smallest Element in a Bst coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/kth-smallest-element-in-a-bst.