Consider a binary tree in which each node has two children except the leaf nodes. If a node is labeled as ‘v’ then its right children will be labeled as 2v+1 and left children as 2v. Root is labelled as
Given two nodes labeled as i and j, the task is to find the shortest distance and the path from i to j. And print the path of node i and node j from root node.
Input : i = 1, j = 2 Output : 1 Path is 1 2 Input : i = 4, j = 3 Output : 3 Path is 4 2 1 3
This problem is mainly an extension of Find distance between two given keys of a Binary Tree. Here we not only find shortest distance, but also the path.
The between the two nodes i and j will be equal to dist(i, LCA(i, j)) + dist(j, LCA(i, j)) where LCA means lowest common ancestor of nodes labeled as i and j. If a number x is represented in binary form then 2*x can be represented by appending 0 to the binary representation of x and 2x+1 can be represented by appending 1 to the binary representation of x. This is because when we append 0 all the terms present in binary form of x shift left, so it gets doubled similarly when we append 1, we get 2x+1. Suppose the binary representation of a node is 1010 this tells us the path of this node from root. First term ‘1’ represents root second term 0 represents left turn then third term 1 represents right turn from previous node and finally 0 represents left turn.
Node 10 in binary form is 1010 and 13 in binary form is 1101 secondly length of binary representation of any node also tells about its level in binary tree. Suppose binary representation of i is m length and is … and binary representation of node j is n length …….
Thus we know the path of i and j from root .Find out k such that for all p<=k = .This is the LCA of i and j in binary form .So dist(i, LCA(i, j)) will be m – k and dist(j, LCA(i, j)) = n – k. so answer will be m + n – 2k. And printing the path is also not a big issue just store the path of i to LCA and path of j to LCA and concatenate them.
1 2 1 4 2 1 3 3
Time Complexity O(i + j)
This article is contributed by Ayush Jha. 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.
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.
- Shortest distance between two nodes in an infinite binary tree
- XOR of path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree | Set 2
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Print the first shortest root to leaf path in a Binary Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Queries to check if the path between two nodes in a tree is a palindrome
- Sum of all odd nodes in the path connecting two given nodes
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Print path from root to all nodes in a Complete Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Minimum sum path between two leaves of a binary tree
- Check if two nodes are on same path in a tree | Set 2
- Print the path common to the two paths from the root to the two given nodes
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Construct Binary Tree from given Parent Array representation
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree