Skip to content

Instantly share code, notes, and snippets.

@ylegall
Created November 9, 2013 05:29
Show Gist options
  • Save ylegall/7382079 to your computer and use it in GitHub Desktop.
Save ylegall/7382079 to your computer and use it in GitHub Desktop.
find the kth smallest element of an in-order traversal of a BST.
int find(Node* root, int k) {
int result = -1;
find(root, k, result);
return result;
}
void find(Node* root, ref int k, ref int result) {
if (root.left) {
find(root.left, k, result);
}
if (!k) {
result = root.val;
}
--k;
if (root.right) {
find(root.right, k, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment