Back to all solutions

#230 - Kth Smallest Element in a BST

Problem Description

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

Solution

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {number}
 */
var kthSmallest = function(root, k) {
  const result = [];
  dfs(root);
  return result[k - 1];

  function dfs(node) {
    if (!node || result.length > k) return null;
    dfs(node.left);
    result.push(node.val);
    dfs(node.right);
  }
};