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 whether the given node is in the path between the nodes U and V
- Check if two nodes are in same subtree of the root node
- Queries for the number of nodes having values less than V in the subtree of a Node
- Print the nodes of binary tree as they become the leaf node
- Print all the nodes except the leftmost node in every level of the given binary tree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Number of edges in a perfect binary tree with N levels
- Sum of all odd nodes in the path connecting two given nodes
- B*-Trees implementation in C++
- AA Trees | Set 1 (Introduction)
- Some Basic Theorems on Trees
- Wavelet Trees | Introduction
- Check if two trees have same structure
- 2-3 Trees | (Search and Insert)
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.