Given a binary tree, the task is to find the distance between two keys in a binary tree, no parent pointers are given. Distance between two nodes is the minimum number of edges to be traversed to reach one node from other.
This problem has been already discussed in previous post but it uses three traversals of the Binary tree, one for finding Lowest Common Ancestor(LCA) of two nodes(let A and B) and then two traversals for finding distance between LCA and A and LCA and B which has O(n) time complexity. In this post, a method will be discussed that requires the O(log(n)) time to find LCA of two nodes.
The distance between two nodes can be obtained in terms of lowest common ancestor. Following is the formula.
Dist(n1, n2) = Dist(root, n1) + Dist(root, n2) - 2*Dist(root, lca) 'n1' and 'n2' are the two given keys 'root' is root of given Binary Tree. 'lca' is lowest common ancestor of n1 and n2 Dist(n1, n2) is the distance between n1 and n2.
Above formula can also be written as:
Dist(n1, n2) = Level[n1] + Level[n2] - 2*Level[lca]
This problem can be breakdown into:
- Finding levels of each node
- Finding the Euler tour of binary tree
- Building segment tree for LCA,
These steps are explained below :
- Find the levels of each node by applying level order traversal.
- Find the LCA of two nodes in binary tree in O(logn) by Storing Euler tour of Binary tree in array and computing two other arrays with the help of levels of each node and Euler tour.
These steps are shown below:
(I) First, find Euler Tour of binary tree.
(II) Then, store levels of each node in Euler array.
(III) Then, store First occurrences of all nodes of binary tree in Euler array. H stores the indices of nodes from Euler array, so that range of query for finding minimum can be minimized and their by further optimizing the query time.
- Then build segment tree on L array and take the low and high values from H array that will give us the first occurrences of say Two nodes(A and B) . Then, <strong>we query segment tree to find the minimum value say X in range (H[A] to H[B]). Then we use the index of value X as index to Euler array to get LCA, i.e. Euler[index(X)].
Let, A = 8 and B = 5.
(I) H = 1 and H =2
(II) Querying on Segment tree, we get min value in L array between 1 and 2 as X=0, index=7
(III) Then, LCA= Euler, i.e LCA = 1.
- Finally, we apply distance formula discussed above to get distance between two nodes.
Dist(4, 5) = 2 Dist(4, 6) = 4 Dist(3, 4) = 3 Dist(2, 4) = 1 Dist(8, 5) = 5
Time Complexity: O(Log N)
Space Complexity: O(N)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
- Find distance between two nodes of a Binary Tree
- Write a program to Calculate Size of a tree | Recursion
- C++ Program to implement Symbol Table
- Iterative Preorder Traversal of an N-ary Tree
- Flatten a binary tree into linked list | Set-2
- Iterative Segment Tree (Range Maximum Query with Node Update)
- Print the DFS traversal step-wise (Backtracking also)
- Queries to find maximum product pair in range with updates
- Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
- Iterative Segment Tree (Range Minimum Query)