please provide some help so as to how i can approach this problem?
Provide me some hint in approaching
you can read this reply
and if you have some doubt or want to ask something
feel free to ask here
https://ide.codingblocks.com/s/322410 i have tried out the way u told me, but there is some error in output, can u plz fix it?
i have removed all compilation error and modified your code
but it is not correct approach and not giving correct output
it is not the approach that i told you please check my code
the only difference b/w ur code and mine is that u have used an extra variable temp of type node* and popped the front of the queue earlier, whereas i didnt use any extra variable and popped the front at the end of loop. But when i’m printing the tree, it goes fine.
also 1 test case is failing sir, if u can please look into it.
NO this is not the case
void helper(node*rt,map<int,pair<int,int>>&hashmap,int level,int hd){
if(rt==NULL)return;
if(hashmap.find(hd)==hashmap.end())
hashmap.insert({hd,{rt->data,level}});
else{
pair<int ,int >p=hashmap[hd];
if(level>=p.second){
hashmap[hd]={rt->data,level};
}
}
helper(rt->left,hashmap,level+1,hd-1);
helper(rt->right,hashmap,level+1,hd+1);
}
In my approach i have used one extra parameter level
which is neccasary
look at this example
20
/ \
8 22
/ \ / \
5 3 4 25
/ \
10 14
For the above tree the output should be 5, 10, 4, 14, 25.
try to run your code for this input you will understand the difference and why it is also important to use level
i cannot understand the code u have written. can u send me a link to the complete code?
i have already shared it with you in my reply
which i refer you to read
Reference Code