The "BST" Judging node R and S is not one of the ancestors and the other for the descendants

Source: Internet
Author: User

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-&gt ; 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.