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
- Check if two nodes are in same subtree of the root node
- Print the nodes of binary tree as they become the leaf node
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Sum of all odd nodes in the path connecting two given nodes
- Search a node in Binary Tree
- Sum of cousins of a given node in a Binary Tree
- Kth ancestor of a node in binary tree | Set 2
- Preorder predecessor of a Node in Binary Tree
- Preorder Successor of a Node in Binary Tree
- Inorder Successor of a node in Binary Tree
- Total sum except adjacent of a given node in a Binary Tree
- Find the maximum node at a given level in a binary tree
- Print the number of set bits in each node of a Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Level Order Successor of a node in Binary Tree
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.
Improved By : _Gaurav_Tiwari