What is the input for which its failing
Input is not shown and output is 2 3
_xenon
#3
Try to use the parent approach to solve this problem… I was facing the same issue before but I was able to solve it using the parent approach.
vector Solution::solve(TreeNode* A, int B) {
vector<int> ans;
map<int,int> par;
if(A==NULL||B==1) return ans;
deque<TreeNode*> q;
q.push_back(A);
q.push_back(NULL);
vector<int> v;
while(!q.empty()){
TreeNode* temp=q.front();
q.pop_front();
if(temp==NULL){
if(find(v.begin(),v.end(),B)!=v.end()){
for(auto f:v){
if(f!=B&&par[f]!=par[B]) ans.push_back(f);
}
return ans;
}
v.clear();
if(!q.empty()){
q.push_back(NULL);
}
}
else{
v.push_back(temp->val);
if(temp->left!=NULL){
par[temp->left->val]=temp->val;
q.push_back(temp->left);
}
if(temp->right!=NULL){
par[temp->right->val]=temp->val;
q.push_back(temp->right);
}
}
}
return ans;
}