How can I find using C if a binary search tree is too tall relatively fast (most of the time)?
To be more specific , lets say I need to find if a tree has a height of at least 10 ,without having to search the whole tree most of the time.
(This is possible because I expect most of the input to be binary search trees which have a heigh greater than 10.)
>Solution :
If there are no preconditions about the structure of the tree, there’s no other way but checking one side of the tree, then the other.
int check_depth(struct tree *root, int depth)
{
if (!root) {
return 0;
} else if (depth <= 1) {
return 1;
} else {
return check_depth(root->left, depth-1) ||
check_depth(root->right, depth-1);
}
}