-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallNodesDistanceKinBinaryTree.cpp
More file actions
76 lines (76 loc) · 2.33 KB
/
allNodesDistanceKinBinaryTree.cpp
File metadata and controls
76 lines (76 loc) · 2.33 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
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// logic -
// step 1 - mark parents of each node so as it traverse in all direction
// - use a map to store parents
// step 2 - find the target node
// step 3 - use queue, traverse in all 3 directions(parent, left & right) upto
// k distance.
// - maintain a visited map as we don't want to traverse parent/already visited node
// step 4 - put queue values in an vector , sort and return.
class Solution {
public:
void markParents(TreeNode* root,unordered_map<TreeNode*,TreeNode*>&mp){
if(root==NULL) return;
queue<TreeNode*>q;
q.push(root);
while(!q.empty()){
TreeNode* curr = q.front();
q.pop();
if(curr->left){
mp[curr->left] = curr;
q.push(curr->left);
}
if(curr->right){
mp[curr->right] = curr;
q.push(curr->right);
}
}
return ;
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
vector<int> ans;
if(root==NULL) return ans;
unordered_map<TreeNode*,TreeNode*> mp;
markParents(root,mp);
queue<TreeNode*>q;
TreeNode* node = target;
q.push(node);
int dist = 0;
unordered_map<TreeNode*,bool> visited;
visited[node] = true;
while(!q.empty()){
if(dist++==k) break;
int qsize = q.size();
for(int i=0;i<qsize;i++){
TreeNode* curr = q.front();
q.pop();
if(curr->left&&!visited[curr->left]){
q.push(curr->left);
visited[curr->left] = true;
}
if(curr->right&&!visited[curr->right]){
q.push(curr->right);
visited[curr->right] = true;
}
if(mp[curr]&&!visited[mp[curr]]){
q.push(mp[curr]);
visited[mp[curr]] = true;
}
}
}
while(!q.empty()){
ans.push_back(q.front()->val);
q.pop();
}
sort(ans.begin(),ans.end());
return ans;
}
};