Given a binary tree with
as its root and for any parent
its left child will be 2*i and right child will be 2*i+1. The task is to find the minimum distance between two nodes n1 and n2.
1 / \ 2 3 / \ / \ 4 5 6 7 / \ / \ / \ / \ . . . . . . . .
Input : n1 = 7, n2 = 10 Output : 5 Input : n1 = 6, n2 = 7 Output : 4
There are so many ways to find the minimum distance between two given nodes of a binary tree.
Here is an efficient way to find the same with the help of binary representation of given nodes. For any node, take a closer look on its binary representation. For example, consider node 9. Binary representation of 9 is 1001. So to find the path from root to a node, find the binary representation of that node and move from left to right in that binary representation and move to right child in tree if a 1 is encountered and move to left child if a 0 is encountered.
Path of 9 as per bit representation 1 / \ 2 3 /\ /\ 4 5 6 7 /\ 8 9.
Hence, for finding the minimum distance between two nodes, we will try to find the common part of binary representation of both nodes which is actually the common path from root to LCA. Let first k-bits for both node is same. Also, if binary form is of n-bit then it shows that path distance from root to that node is of n length. Hence from above statements we can easily conclude that: If m and n is the bit-length of two nodes and k-starting bits of both node’s value are same then the minimum distance between both node will be: m + n – 2*k.
Note: Last similar bit shows the position of LCA in given binary tree.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if all the Nodes in a Binary Tree having common values are at least D distance apart
- Find distance between two nodes of a Binary Tree
- Queries to find distance between two nodes of a Binary tree
- Shortest distance between two nodes in an infinite binary tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Find distance between two nodes in the given Binary tree for Q queries
- Count pairs of leaf nodes in a Binary Tree which are at most K distance apart
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Farthest distance of a Node from each Node of a Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Find distance from root to given node in a binary tree
- Sum of all the child nodes with even parent values in a Binary Tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- 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
- Shortest path between two nodes in array like representation of binary tree
- Print all nodes between two given levels in Binary Tree
- XOR of path between any two nodes in a 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.