Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode ques solution with explanation in comment #369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
//Create child-parent combination in a Map such that we can track easily
void markParents(TreeNode* root, unordered_map<TreeNode*, TreeNode*> &parent_track) {
//make a queue for BFS Traversal
queue<TreeNode*> queue;
//add the root at first
queue.push(root);
while(!queue.empty()) {
//First Node Pull
TreeNode* current = queue.front();
queue.pop();
//if left node exist then add child-parent combination in Map
if(current->left) {
parent_track[current->left] = current;
queue.push(current->left);
}
//if Right node exist then add child-parent combination in Map
if(current->right) {
parent_track[current->right] = current;
queue.push(current->right);
}
}
}
//This is the Given function
vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
//Craete a Map for track all the child-parent combinations
unordered_map<TreeNode*, TreeNode*> parent_track;
//call this function for creating child-parent combination
markParents(root, parent_track);

//Create this map for tracking all the visited node
unordered_map<TreeNode*, bool> visited;
queue<TreeNode*> queue;
queue.push(target);
//mark as visited
visited[target] = true;
int curr_level = 0;
while(!queue.empty()) {
int size = queue.size();
//whenever curr_level == k then break
if(curr_level++ == k) break;
for(int i=0; i<size; i++) {
TreeNode* current = queue.front(); queue.pop();
//if left node exist && not visited then visit and mark as visited
if(current->left && !visited[current->left]) {
queue.push(current->left);
visited[current->left] = true;
}
//if right node exist && not visited then visit and mark as visited
if(current->right && !visited[current->right]) {
queue.push(current->right);
visited[current->right] = true;
}
//reverse back check
if(parent_track[current] && !visited[parent_track[current]]) {
queue.push(parent_track[current]);
visited[parent_track[current]] = true;
}
}
}
//create a vector
vector<int> result;
while(!queue.empty()) {
TreeNode* current = queue.front(); queue.pop();
//add the remaining elements in the queue
result.push_back(current->val);
}
return result;
}

};