Given a binary tree and 3 nodes a, b and c, the task is to find a node in the tree such that after removing all the edge connected to that node, a, b and c are in three different trees.
Given below is a tree with input nodes as c, j and o.
In the above tree, if node i gets disconnected from the tree, then the given nodes c, j, and o will be in three different trees which have been shown below.
A simple approach is to find LCA of all possible pairs of nodes given.
- lca of ( a, b) = x
- lca of (b, c) = y
- lca of (c, a) = z
In any case, either of (x, y), (y, z), (z, x) or (x, y, z) will always be the same. In the first three cases, return the node which is not the same. In the last case returning any node of x, y or z will give the answer.
Below is the implementation of the above approach:
Disconnect node 3 from the tree
- All vertex pairs connected with exactly k edges in a graph
- Maximum number of edges among all connected components of an undirected graph
- Minimize the number of weakly connected nodes
- Remove all outgoing edges except edge with minimum weight
- Find height of a special binary tree whose leaf nodes are connected
- Remove every k-th node of the linked list
- Remove first node of the linked list
- Remove last node of the linked list
- Sum of cousin nodes of a given node in a BST
- Remove all nodes which don't lie in any path with sum>= k
- Print all nodes at distance k from a given node
- Sum of all the parent nodes having child node x
- Find all reachable nodes from every node present in a given set
- Print all nodes that are at distance k from a leaf node
- Identify all Grand-Parent Nodes of each Node in a Map
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.