I'm stuck with a recursive function to find the depth of a node in a binary tree, more specifically in the else condition:
If the tree was a binary search tree, knowing that the left child value is always lower than the parent one and that the right child is always higher, I could add an if condition so that if the node x value is inferior to the root I always return with root->leftchild and viceversa, but because the tree is not a binary search one I have to check both left and right and I'm stuck with two consecutive return in the else condition.
When looking at the function, assume that the node always exist,that the node x is never the root and the passed depth at the beginning is always 0.
int node_depth (Node x,Node root,int depth){
if(x->parent==root){
return depth;
}
else{
return node_depth(x,root->leftchild,depth+1);
return node_depth(x,root->rightchild,depth+1);
}
}
If tree was binary search:
else{
if(x->value<root->value){
return node_depth(x,root->leftchild,depth+1);
}
if(x->value>root->value){
return node_depth(x,root->rightchild,depth+1);
}
}