-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistanceBetweenNode.cpp
More file actions
64 lines (64 loc) · 1.61 KB
/
distanceBetweenNode.cpp
File metadata and controls
64 lines (64 loc) · 1.61 KB
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
#include<iostream>
#include<queue>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* findLCA(TreeNode* root, int B, int C){
if(!root){
return NULL;
}
if(root->val>B && root->val<C){
//found it
return root;
}
else if(root->val==B || root->val==C){
return root;
}
TreeNode *returnValue;
if(root->val<B){
returnValue = findLCA(root->right,B,C);
}
else if(root->val>C){
returnValue = findLCA(root->left, B,C);
}
return returnValue;
}
int solve(TreeNode* A, int B, int C){
//find lowest common ancestor of B and C;
int distance = 0;
TreeNode *root = findLCA(A,B,C);
//from root find distance of A and B;
//use level order traversal
//total distance will be distance of A + distance of B;
std::queue<TreeNode*> q;
q.push(root);
bool Bfound = false,Cfound =false;
int Bdistance = 0,Cdistance = 0;
int level = -1;
while(!q.empty()){
int currLevelSize = q.size();
int index = 0;
++level;
while(index<currLevelSize){
TreeNode* currNode = q.front();
q.pop();
if(!Bfound&&currNode->val==B){
Bfound = true;
Bdistance = level;
}
if(!Cfound&&currNode->val==C){
Cfound = true;
Cdistance = level;
}
++index;
}
if(Bfound && Cfound){
break;
}
}
distance = Bdistance + Cdistance;
return distance;
}