I am looking at the Geeks for Geeks problem Burning Tree:
Given a binary tree and a node called target. Find the minimum time required to burn the complete binary tree if the target is set on fire. It is known that in 1 second all nodes connected to a given node get burned. That is its left child, right child, and parent.
I am getting the following error:
Abort signal from abort(3) (SIGABRT)
This is my code:
class Solution {
// mapping to parent
// returns the target node
Node* createParentMapping(Node* root, int target,
map<Node*, Node*> &nodeToParent) {
Node* res = NULL;
queue<Node*> que;
que.push(root);
nodeToParent[root] = NULL;
while (!que.empty()) {
Node* front = que.front();
if (front->data == target) {
res = front;
}
if (front->left) {
nodeToParent[front->left] = front;
que.push(front->left);
}
if (front->right) {
nodeToParent[front->right] = front;
que.push(front->right);
}
}
return res;
}
// burns the tree
int burnTree(Node* root, map<Node*, Node*> &nodeToParent) {
map<Node*, bool> visited;
queue<Node*> q;
q.push(root);
visited[root] = true;
int ans = 0;
while (!q.empty()) {
bool flag = 0;
int size = q.size();
for(int i = 0; i < size; i++) {
Node* front = q.front();
q.pop();
if (front->left && !visited[front->left]) {
flag = 1;
q.push(front->left);
visited[front->left] = 1;
}
if (front->right && !visited[front->right]) {
flag = 1;
q.push(front->right);
visited[front->right] = 1;
}
if (nodeToParent[front] && !visited[nodeToParent[front]]) {
flag = 1;
q.push(nodeToParent[front]);
visited[nodeToParent[front]] = 1;
}
}
if (flag == 1) {
ans++;
}
}
return ans;
}
public:
int minTime(Node* root, int target) {
map<Node*, Node*> NodeToParent;
Node* targetNode = createParentMapping(root, target, NodeToParent);
int time = burnTree(targetNode, NodeToParent);
return time;
}
};
I am not able to find out the where the problem lies. What am I missing?