- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find distance between two nodes of a Binary Tree in C++ Program
In this problem, we are given a binary tree and two nodes. Our task is to create a program to Find distance between two nodes of a Binary Tree.
Problem Description
We need to find the distance between two nodes which is the minimum number of edges that will be traversed when we go from one node to another node.
Let’s take an example to understand the problem,
Input: binary tree
Node1 = 3 ,Node2 = 5
Output: 3
Explanation
The path from node 3 to node 5 is 3 -> 1 -> 2 -> 5. There are 3 edges traversed that make distance 3.
Solution Approach
A simple solution to the problem is using the lowest common ancestor node for the given nodes and then apply the below formula,
distance(node1, node2) = distance(root, node1) + distance(root, node2) + distance(root, LCA)
Example
#include <iostream> using namespace std; struct Node{ struct Node *left, *right; int key; }; Node* insertNode(int key){ Node *temp = new Node; temp->key = key; temp->left = temp->right = NULL; return temp; } int calcNodeLevel(Node *root, int val, int level) { if (root == NULL) return -1; if (root->key == val) return level; int lvl = calcNodeLevel(root->left, val, level+1); return (lvl != -1)? lvl : calcNodeLevel(root->right, val, level+1); } Node *findDistanceRec(Node* root, int node1, int node2, int &dist1, int &dist2, int &dist, int lvl){ if (root == NULL) return NULL; if (root->key == node1){ dist1 = lvl; return root; } if (root->key == node2){ dist2 = lvl; return root; } Node *leftLCA = findDistanceRec(root->left, node1, node2, dist1,dist2, dist, lvl+1); Node *rightLCA = findDistanceRec(root->right, node1, node2, dist1,dist2, dist, lvl+1); if (leftLCA && rightLCA){ dist = dist1 + dist2 - 2*lvl; return root; } return (leftLCA != NULL)? leftLCA: rightLCA; } int CalcNodeDistance(Node *root, int node1, int node2) { int dist1 = -1, dist2 = -1, dist; Node *lca = findDistanceRec(root, node1, node2, dist1, dist2, dist, 1); if (dist1 != -1 && dist2 != -1) return dist; if (dist1 != -1){ dist = calcNodeLevel(lca, node2, 0); return dist; } if (dist2 != -1){ dist = calcNodeLevel(lca, node1, 0); return dist; } return -1; } int main(){ Node * root = insertNode(1); root->left = insertNode(2); root->right = insertNode(3); root->left->left = insertNode(4); root->left->right = insertNode(5); root->right->left = insertNode(6); cout<<"Distance between node with value 5 and node with value 3 is"<<CalcNodeDistance(root, 3, 5); return 0; }
Output
Distance between node with value 5 and node with value 3 is 3
- Related Articles
- Find distance between two nodes of a Binary Tree in C++
- Program to find out distance between two nodes in a binary tree in Python
- Queries to find distance between two nodes of a Binary tree – O(logn) method in C++
- Program to print nodes between two given level numbers of a binary tree using C++
- All Nodes Distance K in Binary Tree in C++
- XOR of the path between any two nodes in a Binary Tree in C++
- Program to find the largest sum of the path between two nodes in a binary tree in Python
- Print path between any two nodes in a Binary Tree in C++ Programming.
- Print all nodes between two given levels in Binary Tree in C++
- C++ program to find the shortest distance between two nodes in BST
- Program to find longest path between two nodes of a tree in Python
- Print leftmost and rightmost nodes of a Binary Tree in C Program.
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Validate Binary Tree Nodes in C++
- Program to find leaf and non-leaf nodes of a binary tree in Python

Advertisements