Given a tree (not necessarily a binary tree) and a number of queries such that every query takes two nodes of tree as parameters. For every query pair, find if two nodes are on the same path from root to the bottom.
For example, consider the below tree, if given queries are (1, 5), (1, 6) and (2, 6), then answers should be true, true and false respectively.
Note that 1 and 5 lie on same root to leaf path, so do 1 and 6, but 2 and 6 are not on same root to leaf path.
It is obvious that Depth First Search technique is to be used to solve above problem, the main problem is how to respond to multiple queries fast. Here our graph is a tree which may have any number of children. Now DFS in a tree if started from root node proceeds in a depth search manner i.e. Suppose root has three children and those children have only one child with them so if DFS is started then it first visits the first child of root node then will go deep to the child of that node. The situation with a small tree can be shown as follows:
The order of visiting the nodes will be – 1 2 5 3 6 4 7 .
Thus other children nodes are visited later until completely one child is successfully visited till depth. To simplify this if we assume that we have a watch in our hand and we start walking from root in DFS manner.
Intime – When we visit the node for the first time
Outtime- If we again visit the node later but there is no children unvisited we call it outtime,
Note: Any node in its sub-tree will always have intime < its children (or children of children) because it is always visited first before children (due to DFS) and will have outtime > all nodes in its sub-tree because before noting the outtime it waits for all of its children to be marked visited.
For any two nodes u, v if they are in same path then,
Intime[v] < Intime[u] and Outtime[v] > Outtime[u] OR Intime[u] < Intime[v] and Outtime[u ]> Outtime[v]
- If given pair of nodes follows any of the two conditions, then they are on the same root to leaf path.
- Else not on same path (If two nodes are on different paths it means that no one is in subtree of each other).
We use a global variable time which will be incremented as dfs for a node begins and will also be incremented after
DFS(v) increment timer Intime[v] = timer mark v as visited for all u that are children of v DFS(u) increment timer Outtime[v] = timer end
Time Complexity – O(n) for preprocessing and O(1) per query.
Below is implementation of above pseudo code.
Yes Yes No
From the diagram below to understand more we can have some examples. DFS algorithm as modified above will result in the following intime and outtime for the vertex of the tree as labelled there. Now we will consider all the cases.
Case 1 : Nodes 2 and 4 : Node 2 has intime less than node 4 but since 4 is in its sub tree so it will have a greater exit time than 4 . Thus condition is valid and both are on same path.
Case 2 : Nodes 7 and 6 : Node 7 has intime less than node 6 but since both nodes are not in each other’s sub tree so their exit time does not follow the required condition.
This article is contributed by Saurabh Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Print the path between any two nodes of a tree | DFS
- Check if given path between two nodes of a graph represents a shortest paths
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Minimum cost to reverse edges such that there is path between every pair of nodes
- XOR of all the nodes in the sub-tree of the given node
- Subtree of all nodes in a tree using DFS
- Number of special nodes in an n-ary tree
- Count the nodes in the given tree whose weight is even
- Count the nodes in the given tree whose weight is even parity
- Find distance between two nodes of a Binary Tree
- Count the nodes in the given tree whose weight is a power of two
- Count the nodes in the given tree whose weight is prime
- Count the nodes in the given tree whose sum of digits of weight is odd
- Print Nodes in Top View of Binary Tree
- Count the nodes of the given tree whose weight has X as a factor