-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find distance between to nodes - BT.txt
95 lines (80 loc) · 2.42 KB
/
Find distance between to nodes - BT.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
The distance between two nodes is the minimum number of edges to be traversed to reach one node from another.
Better Solution :
We first find the LCA of two nodes. Then we find the distance from LCA to two nodes.
/* C++ Program to find distance between n1 and n2
using one traversal */
#include <iostream>
using namespace std;
// A Binary Tree Node
struct Node {
struct Node *left, *right;
int key;
};
// Utility function to create a new tree Node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}
Node* LCA(Node* root, int n1, int n2)
{
// Your code here
if (root == NULL)
return root;
if (root->key == n1 || root->key == n2)
return root;
Node* left = LCA(root->left, n1, n2);
Node* right = LCA(root->right, n1, n2);
if (left != NULL && right != NULL)
return root;
if (left == NULL && right == NULL)
return NULL;
if (left != NULL)
return LCA(root->left, n1, n2);
return LCA(root->right, n1, n2);
}
// Returns level of key k if it is present in
// tree, otherwise returns -1
int findLevel(Node* root, int k, int level)
{
if (root == NULL)
return -1;
if (root->key == k)
return level;
int left = findLevel(root->left, k, level + 1);
if (left == -1)
return findLevel(root->right, k, level + 1);
return left;
}
int findDistance(Node* root, int a, int b)
{
// Your code here
Node* lca = LCA(root, a, b);
int d1 = findLevel(lca, a, 0);
int d2 = findLevel(lca, b, 0);
return d1 + d2;
}
// Driver program to test above functions
int main()
{
// Let us create binary tree given in
// the above example
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->left->right = newNode(8);
cout << "Dist(4, 5) = " << findDistance(root, 4, 5);
cout << "\nDist(4, 6) = " << findDistance(root, 4, 6);
cout << "\nDist(3, 4) = " << findDistance(root, 3, 4);
cout << "\nDist(2, 4) = " << findDistance(root, 2, 4);
cout << "\nDist(8, 5) = " << findDistance(root, 8, 5);
return 0;
}
Time Complexity: O(n), As the method does a single tree traversal. Here n is the number of elements in the tree.
Auxiliary Space: O(h), Here h is the height of the tree and the extra space is used in recursion call stack.