But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.
You're in!
Write a function to find the 2nd largest element in a binary search tree.
Here's a sample binary tree implementation:
typedef struct BinaryTreeNode {
void *value;
struct BinaryTreeNode *left;
struct BinaryTreeNode *right;
} BinaryTreeNode;
BinaryTreeNode * binaryTreeNodeNew(const void *value, size_t valueSize)
{
BinaryTreeNode *node;
node = malloc(sizeof(BinaryTreeNode));
assert(node != NULL);
node->value = malloc(valueSize);
assert(node->value != NULL);
memcpy(node->value, value, valueSize);
node->left = NULL;
node->right = NULL;
return node;
}
BinaryTreeNode * binaryTreeNodeInsertLeft(BinaryTreeNode *treeRoot, const void *value, size_t valueSize)
{
treeRoot->left = binaryTreeNodeNew(value, valueSize);
return treeRoot->left;
}
BinaryTreeNode * binaryTreeNodeInsertRight(BinaryTreeNode *treeRoot, const void *value, size_t valueSize)
{
treeRoot->right = binaryTreeNodeNew(value, valueSize);
return treeRoot->right;
}
void binaryTreeNodeFree(BinaryTreeNode *treeRoot)
{
if (treeRoot != NULL) {
binaryTreeNodeFree(treeRoot->left);
binaryTreeNodeFree(treeRoot->right);
free(treeRoot->value);
free(treeRoot);
}
}
Log in or sign up with one click to get immediate access to 3 free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
We're doing one walk down our BST, which means time, where is the height of the tree (again, that's if the tree is balanced, otherwise). space.
Log in or sign up with one click to get immediate access to 3 free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Reset editor
Powered by qualified.io