- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Remove Edges Connected to a Node Such That The Three Given Nodes are in Different Trees Using C++

Suppose we are given a binary tree and three nodes in that binary. We have to disconnect one node entirely from the tree. Disconnecting that node leaves us with three different trees. Each of the three given nodes lies in one of them, or each of the given three nodes must not exist in the same tree.

Disconnecting a node means that we will remove all the edges from this node to all other nodes.

## Example

For example, let's say we have a tree with three nodes 18, 15, and 17 as shown below −

If the task is to remove the node 12 we have to cut the links of this with the sub trees as:

After the removal the three trees will be

T1: {14, 18, 19}

T2: {15},

T3: {11,12,16,17}

We can see that we got three trees and all the three nodes are in 3 different trees by disconnecting element two from the tree. The given **C++ code** shows that the **Least Common Ancestor** of nodes 18 and 15 is 12. So removing nodes at the top of 12 will not work. LCA of nodes 15 and 17 is 11, and LCA of nodes 18 and 17 is 11

As we observe,

LCA(18, 15) = 12

LCA(15, 17) =11

LCA(18, 17) = 11

As we observe that one comes in both (15,17) and (18,17), deleting one will not work as the other two will still be connected. 12 is coming only once, and disconnecting this will disconnect both other nodes. Disconnecting this node will also disconnect the LCA of 11. These two were causing the same LCA, and now they have not connected themselves. If you are lost here, try to take an example and solve it yourself.

So all we need to do is find the LCA of all three nodes among themselves and take nonrepeating one,

Formally speaking, If −

**LCA(a, b) = l**

**LCA(b, c) = m**

**LCA(a, c) = k**

Then **l==m, k** is the answer, if m==k, then l is the answer and if l==k, then m is the answer.

**Note** − It's a bit tricky to wrap your head around this, so I recommend taking some examples and solving them yourself.

#include <iostream> using namespace std; class Node { public: int value; Node *left, *right; Node(int value) { this->value = value; left = right = NULL; } }; Node* lca(Node* root, int key1, int key2) { if (root == NULL) { return NULL; } if (root->value == key1 || root->value == key2) { return root; } Node* llca = lca(root->left, key1, key2); Node* rlca = lca(root->right, key1, key2); if (llca && rlca) { return root; } return (llca != NULL) ? llca : rlca; } Node* solve(Node* root, int a, int b, int c) { Node* l = lca(root, a, b); Node* m = lca(root, b, c); Node* k = lca(root, c, a); if (l->value == m->value) { return k; } else if (l->value == k->value) { return m; } else { return l; } } int main() { Node* root = new Node(11); root->left = new Node(12); root->right = new Node(13); root->left->left = new Node(14); root->left->right = new Node(15); root->left->left->left = new Node(18); root->left->left->right = new Node(19); root->right->left = new Node(16); root->right->right = new Node(17); cout << "Disconnect node with value: " << solve(root, 18, 15, 17)->value; return 0; }

## Output

Disconnect node with value: 12

## Conclusion

The time complexity of our approach depends on the LCA algorithm. So, LCA has O(n) time complexity, and we perform it three times. So O(3*n), which is O(n). After working with some observations and examples, we can spot the LCA and connect the tree. After this, the problem becomes a simple problem to calculate the LCA of the given tree with given nodes.- Related Questions & Answers
- C++ Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
- Find three element from different three arrays such that that a + b + c = sum in Python
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal in C++
- Maximum subsequence sum such that no three are consecutive
- How to remove all child nodes from a parent node using jQuery?
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree in C++
- Find pairs with given sum such that elements of pair are in different rows in Python
- Print all nodes that are at distance k from a leaf node in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Print all nodes at distance k from a given node in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- Find pairs with given sum such that pair elements lie in different BSTs in Python
- Assign weights to edges such that longest path in terms of weights is minimized in C++
- Find height of a special binary tree whose leaf nodes are connected in C++
- Delete all the nodes from the doubly linked list that are greater than a given value in C++