Title: EPI
I think the code in the book is wrong. My code looks like this, the time complexity is O (h), and H is the height of the two fork tree.
The function does not allow r and S to be nullptr, and requires that R, M, s three pointers are not equal to bool Is_r_s_descendant_and_ancestor (shared_ptr<treenode> R, shared_ptr <treenode> m, shared_ptr<treenode> s) {if (m = = Nullptr | | r = = NULLPTR | | s = = nullptr) return false;shared_ptr <treenode> Cur_r = r, cur_s = s, cur_m = M;bool Is_r = False, is_s = False;//is_r means M is not a descendant of R, <span style= "Font-fa Mily:arial, Helvetica, Sans-serif; >is_s represents that M is not a descendant of S </span>//starting with R and S, while looking down Mwhile (cur_r && cur_s) {if (Cur_r = m) {is_r = True;break;} if (cur_s = = m) {is_s = True;break;} Cur_r = cur_r->data > M->data? cur_r->left:cur_r->right;cur_s = cur_s->data > M->data? Cur_s->left:cur_s->right;} if (!is_r &&!is_s)//Two none found M{while (cur_r) {if (Cur_r = m) {is_r = True;break;} Cur_r = cur_r->data > M->data? Cur_r->left:cur_r->right;} while (cur_s) {if (cur_s = = m) {is_s = True;break;} cur_s = cur_s->data > M->data? Cur_s->left:cur_s->right;} if (!is_r &&!is_s)//Still not found return false;} if (Is_r)//r descendants have m{//to determine if M is not s ancestor while (cur_m) {if (cur_m = = s) return true;cur_m = cur_m->data > s->data? cur_m-> ; left:cur_m->right;} return false;} if (is_s) {while (cur_m) {if (cur_m ==r) return true;cur_m = cur_m->data > r->data? cur_m->left:cur_m->righ t;} return false;}}
The "BST" Judging node R and S is not one of the ancestors and the other for the descendants