Write a recursive function that returns the number of right children in a binary
ID: 3809648 • Letter: W
Question
Write a recursive function that returns the number of right children in a binary search tree. Function prototype: int BST::numberOfRightChildren (BSTNode * p) const Deliverable: Write the code for each question using cpp.sh then generate a unique URL after compiling the code. Then include that URL in your Word or PDF file that you submit. Alternatively, you can paste screenshot of the source code of each question that you did using a C++ IDE such as Visual C++ or other. But do not write the code in word file directly.Explanation / Answer
int BST<T>::numberOfRightChildren(BSTNode<T> *p) {
if(p == NULL)
return 0;
int count = 0;
// if this node has any right child,
// recursively calculate how many of them are there..
// which are right child of their parent
if(p->right != NULL) {
count += 1 + numberOfRightChildren(p->right);
}
// check if there are any nodes in left subtree,
// which are right child of their parent
if(p->left != NULL) {
count += numberOfRightChildren(p->left);
}
// return final count
return count;
}