The method of C + + to find the lowest common parent node _c language

Source: Internet
Author: User

This paper illustrates the method of finding the lowest common parent node in C + +, and is the classical algorithm of binary tree in data structure. Share to everyone for your reference. The specific methods are as follows:

Minimum public parent node, meaning very well understood.

Idea 1: The lowest public parent node satisfies the condition that two nodes are located in their Saozi right subtree, then two bool variables, Leftflag and rightflag are defined, and if in the left subtree, Leftflag is true, if in the right subtree, Rightflag is true to satisfy the condition only if Leftflag = = Rightflag = = True.

The implementation code is as follows:

#include <iostream> using namespace std; struct Node {node (int i = 0, node *pleft = NULL, node *pright = null): Data (i), left (Pleft), right (Pright) {} Node *l
 Eft
 Node *right;
int data;

};
 Node *constructnode (node **pnode1, node **pnode2) {node *node12 = new node (12);
 Node *node11 = new node (11);
 Node *node10 = new node (10);
 Node *node9 = new Node (9, NULL, node12);
 Node *node8 = new Node (8, Node11, NULL);
 Node *node7 = new node (7);
 Node *node6 = new node (6);
 Node *NODE5 = new node (5, Node8, NODE9);
 Node *node4 = new Node (4, Node10);
 Node *node3 = new Node (3, Node6, Node7);
 Node *node2 = new Node (2, Node4, NODE5);

 Node *node1 = new node (1, Node2, NODE3);
 *pnode1 = Node6;

 *pnode2 = Node12;
return node1;  BOOL Isnodein (node *root, node *node1, node *node2) {if (Node1 = null | | node2 = NULL) {throw ("Invalid Node1 and
 Node2 ");
 return false;

 if (root = NULL) return false;
 if (root = = Node1 | | | root = node2) {return true; else {RETurn Isnodein (Root->left, Node1, node2) | |
 Isnodein (Root->right, Node1, Node2); Node *lowestfarther (node *root, node *node1, node *node2) {if (root = null | | node1 = NULL | | node2 = NULL | | no
 De1 = = Node2) {return NULL;
 BOOL Leftflag = false;
 BOOL Rightflag = false;
 Leftflag = Isnodein (Root->left, Node1, Node2);

 Rightflag = Isnodein (Root->right, Node1, Node2);
 if (Leftflag = = True && Rightflag = = True) {return root;
 else if (Leftflag = = True) {return Lowestfarther (Root->left, Node1, Node2);
 else {return Lowestfarther (root->right, Node1, Node2);
 } void Main () {Node *node1 = NULL;
 Node *node2 = NULL;

 Node *root = Constructnode (&node1, &node2);
 cout << "Node1:" << node1->data << Endl;
 cout << "Node2:" << node2->data << Endl;

 cout << "root:" << root->data << Endl;

 Node *father = Lowestfarther (Root, Node1, node2);
 if (father = NULL){cout << "no common father" << Endl;
 else {cout << "father:" << father->data << Endl;

 }
}

This type of problem is often encountered during an interview, and the following situations need to be considered:

1. Node1 and Node2 point to the same node, how does this work
2. Is there a possibility that node1 or node2 is not a leaf node?
3. Node1 or Node2 must be in the tree?

Also consider an efficiency issue, which uses two recursive functions and an unnecessary recursive process, thinking carefully, in fact, a recursive process is sufficient to solve this problem

The implementation code is as follows:

#include <iostream> using namespace std; struct Node {node (int i = 0, node *pleft = NULL, node *pright = null): Data (i), left (Pleft), right (pright) {} int dat
 A
 Node *left;
Node *right;

}; 
 Node *constructnode (node **pnode1, node **pnode2) {node *node12 = new node (12); 
 Node *node11 = new node (11); 
 Node *node10 = new node (10); 
 Node *node9 = new Node (9, NULL, node12); 
 Node *node8 = new Node (8, Node11, NULL); 
 Node *node7 = new node (7); 
 Node *node6 = new node (6); 
 Node *NODE5 = new node (5, Node8, NODE9); 
 Node *node4 = new Node (4, Node10); 
 Node *node3 = new Node (3, Node6, Node7); 
 Node *node2 = new Node (2, Node4, NODE5); 

 Node *node1 = new node (1, Node2, NODE3); 
 *pnode1 = Node6; 

 *pnode2 = NODE5; 
return node1; BOOL Lowestfather (node *root, node *node1, node *node2, node *&dest) {if (root = null | | node1 = NULL | | node2 = = NULL | |
 Node1 = = Node2) return false;

 if (root = = Node1 | | | root = = NODE2) return true; BOOL Leftflag = LowesTfather (Root->left, Node1, Node2, dest);
 
 BOOL Rightflag = Lowestfather (Root->right, Node1, Node2, dest);
 if (Leftflag = = True && Rightflag = = True) {dest = root;
} if (Leftflag = = True | | rightflag = = true) return true;
 int main () {Node *node1 = NULL;
 Node *node2 = NULL;

 Node *root = Constructnode (&node1, &node2);
 BOOL Flag1 = false;
 BOOL Flag2 = false;
 Node *dest = NULL;

 BOOL flag = Lowestfather (root, Node1, Node2, dest);
 if (dest!= NULL) {cout << "lowest common father:" << dest->data << Endl;
 else {cout << "no common father!" << Endl;
return 0;

 }

A different way to do the following:

#include <iostream> using namespace std; struct Node {node (int i = 0, node *pleft = NULL, node *pright = null): Data (i), left (Pleft), right (pright) {} int dat
 A
 Node *left;
Node *right;

}; 
 Node *constructnode (node **pnode1, node **pnode2) {node *node12 = new node (12); 
 Node *node11 = new node (11); 
 Node *node10 = new node (10); 
 Node *node9 = new Node (9, NULL, node12); 
 Node *node8 = new Node (8, Node11, NULL); 
 Node *node7 = new node (7); 
 Node *node6 = new node (6); 
 Node *NODE5 = new node (5, Node8, NODE9); 
 Node *node4 = new Node (4, Node10); 
 Node *node3 = new Node (3, Node6, Node7); 
 Node *node2 = new Node (2, Node4, NODE5); 

 Node *node1 = new node (1, Node2, NODE3); 
 *pnode1 = Node11; 

 *pnode2 = Node12; 
return node1;  } node* Lowestfather (node *root, node *node1, node *node2) {if (root = null | | node1 = NULL | | node2 = NULL | | node1
 = = Node2) return NULL;

 if (root = = Node1 | | | root = = node2) return root; node* Leftflag = Lowestfather (root-&Gt;left, Node1, Node2);

 node* Rightflag = Lowestfather (Root->right, Node1, Node2);
 if (Leftflag = NULL) return rightflag;
 else if (Rightflag = NULL) return leftflag;
else return root;
 int main () {Node *node1 = NULL;
 Node *node2 = NULL;

 Node *root = Constructnode (&node1, &node2);
 BOOL Flag1 = false;
 BOOL Flag2 = false;
 Node *dest = NULL;

 node* flag = Lowestfather (root, Node1, node2);
 if (flag!= NULL) {cout << "lowest common father:" << flag->data << Endl;
 else {cout << "no common father!" << Endl;
return 0;

 }

I hope this article will help you with your study of C + + program design.

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.