Created
November 9, 2013 05:29
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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