Minimum distance between two given nodes in an N-ary tree
Given a N ary Tree consisting of N nodes, the task is to find the minimum distance from node A to node B of the tree.
Examples:
Input:
1
/ \
2 3
/ \ / \ \
4 5 6 7 8
A = 4, B = 3
Output: 3
Explanation: The path 4->2->1->3 gives the minimum distance from A to B.Input:
1
/ \
2 3
/ \ \
6 7 8
A = 6, B = 7
Output: 2
Approach: This problem can be solved by using concept of LCA(Lowest Common Ancestor). Minimum distance between two given nodes A and B can be found out by using formula –
mindistance(A, B) = dist (LCA, A) + dist (LCA, B)
Follow the steps below to solve the problem:
- Find path from root node to the nodes A and B, respectively and simultaneously store the two paths in two arrays.
- Now iterate until values of both the arrays are same and value just before mismatch is the LCA node value.
- The value just before mismatch is the LCA node value.
- Find distance from the LCA node to node A and B, which can be found with the given steps:
- In first array, iterate from LCA node value and increase count until value of node A is found, which is dist (LCA, A).
- In the second array, iterate from LCA node value and increase count until value of node B is found, which is dist (LCA, B)
- Return the sum of those distances i.e dist (LCA, A) + dist (LCA, B).
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Structure of Nodestruct Node { int val; vector<Node*> child;}; // Utility function to create a// new tree nodeNode* newNode(int key){ Node* temp = new Node; temp->val = key; return temp;} bool flag; // Function to get the path// from root to a nodevoid findPath(Node* root, int key, vector<int>& arr){ if (!root) return; arr.push_back(root->val); // if key is found set flag and return if (root->val == key) { flag = 1; return; } // recur for all children for (int i = 0; i < root->child.size(); i++) { findPath(root->child[i], key, arr); // if key is found dont need to pop values if (flag == 1) return; } arr.pop_back(); return;} void findMinDist(Node* root, int A, int B){ if (root == NULL) return; int val = root->val; // vector to store both paths vector<int> arr1, arr2; // set flag as false; flag = false; // find path from root to node a findPath(root, A, arr1); // set flag again as false; flag = false; // find path from root to node b findPath(root, B, arr2); // to store index of LCA node int j; // if unequal values are found // return previous value for (int i = 1; i < min(arr1.size(), arr2.size()); i++) { if (arr1[i] != arr2[i]) { val = arr1[i - 1]; j = i - 1; break; } } int d1 = 0, d2 = 0; // iterate for finding distance // between LCA(a, b) and a for (int i = j; i < arr1.size(); i++) if (arr1[i] == A) break; else d1 += 1; // iterate for finding distance // between LCA(a, b) and b for (int i = j; i < arr2.size(); i++) if (arr2[i] == B) break; else d2 += 1; // get distance val = d1 + d2; cout << val << '\n';} // Driver Codeint main() { Node* root = newNode(1); (root->child).push_back(newNode(2)); (root->child).push_back(newNode(3)); (root->child[0]->child).push_back(newNode(4)); (root->child[0]->child).push_back(newNode(5)); (root->child[1]->child).push_back(newNode(6)); (root->child[1])->child.push_back(newNode(7)); (root->child[1]->child).push_back(newNode(8)); int A = 4, B = 3; // get min distance findMinDist(root, A, B); return 0;} |
3
Time complexity: O(N).
Auxiliary Space: O(N).
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


