Given a binary tree having N nodes and weight of N-1 edges. The distance between two nodes is the sum of the weight of edges on the path between two nodes. Each query contains two integers U and V, the task is to find the distance between nodes U and V.
Output: 3 5 12 12
Distance between nodes 1 to 3 = weight(1, 3) = 2
Distance between nodes 2 to 3 = weight(1, 2) + weight(1, 3) = 5
Distance between nodes 3 to 5 = weight(1, 3) + weight(1, 2) + weight(2, 5) = 12
Distance between nodes 4 to 5 = weight(4, 2) + weight(2, 5) = 12
- Binary Lifting is a Dynamic Programming approach where we pre-compute an array lca[i][j] where i = [1, n], j = [1, log(n)] and lca[i][j] contains 2j-th ancestor of node i.
- For computing the values of lca, the following recursion may be used
- As we will compute the lca array we will also calculate the distance where distance[i][j] contains the distance from node i to its 2j-th ancestor
- For computing the values of dist, the following recursion may be used.
- After precomputation, we find the distance between (u, v) as we find the least common ancestor of (u, v).
Below is the implementation of the above approach:
3 5 12
Time Complexity: The time taken in pre-processing is O(N logN) and every query takes O(logN) time. Therefore, overall time complexity of the solution is O(N logN).
- Queries to find distance between two nodes of a Binary tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Find distance between two nodes of a Binary Tree
- Find distance of nodes from root in a tree for multiple queries
- Shortest distance between two nodes in an infinite binary tree
- Distance between two nodes of binary tree with node values from 1 to N
- Binary Array Range Queries to find the minimum distance between two Zeros
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Queries to check if the path between two nodes in a tree is a palindrome
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Find the shortest distance between any pair of two different good nodes
- Count pairs of leaf nodes in a Binary Tree which are at most K distance apart
- Check if all the Nodes in a Binary Tree having common values are at least D distance apart
- Count of all prime weight nodes between given nodes in the given Tree
- Find the path from root to the given nodes of a tree for multiple queries
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Print nodes between two given level numbers of a binary tree
- Print all nodes between two given levels in Binary Tree
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
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.