The least common ancestor problem of binary tree

Source: Internet
Author: User

Today did two two fork tree topic, quite simple, without debugging, directly on the submission box to complete the submission, directly passed.

The first topic is to seek the common ancestor of the binary lookup tree, because it is sort, so it is good to do.

Specific ideas are as follows:

1 if the two nodes are smaller than the current node, then the common ancestor must be on the left subtree of the current node, so recursion Zuozi;

2 If two nodes are larger than the current node, then ibid, recursive right subtree;

3 If the two nodes have one for the current node, then the current node is a public node; If two nodes are smaller and larger than the current node, the current node must be a public node.


The specific code is as follows:

Class Solution {public:    treenode* lowestcommonancestor (treenode* root, treenode* p, treenode* q) {        if (root = = NUL L | | p = = NULL | | Q = = NULL)            return root;        if (P->val > Root->val && q->val > Root->val)            return Lowestcommonancestor (root->right , p, q);        else if (P->val < root->val && Q->val < Root->val)            return Lowestcommonancestor (root-> Left, p, q);        else            return root;    };


The second topic is to seek the common ancestor of the binary tree, because it is not a sort, so it is a little more complex than the above, the general idea is: until the recursive to a certain node to meet certain conditions, return the node; Otherwise, continue to recursive, if to the leaf node, return null. Then the final return must be two of the common ancestor of the nodes.

Specific ideas:

1 returns null if the current node is empty;

2 If the current node is one of two nodes, or the current node is exactly the parent node of the two node, then the node is returned;

3 recursively traverse the left subtree of the current node;

4 recursively traverse the right subtree of the current node;

5 if both 3 and 4 are not empty, then the current node must be a public ancestor, returning the current node;

6 If 5 is not satisfied, but 3 returns no null, then returns the node pointer of 3;

7 If 5 is not satisfied, but 4 returns a non-empty, 4 returns the node pointer.


The specific code is as follows:

Class Solution {public:    treenode* lowestcommonancestor (treenode* root, treenode* p, treenode* q) {        if (root = = NUL L)            return NULL;        if (root = = P | | root = = Q | | (Root->left = = P && root->right = = q) | | (Root->left = = Q && root->right = = p))            return root;        treenode* Leftreturn = Lowestcommonancestor (Root->left, p, q);        treenode* Rightreturn = Lowestcommonancestor (Root->right, p, q);        if (Leftreturn! = NULL && Rightreturn! = null)            return root;        if (Leftreturn! = NULL)            return leftreturn;        if (Rightreturn! = NULL)            return rightreturn;    }};



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The least common ancestor problem of binary tree

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.