vsohail09
BAN USERU should do
int binarytree_search(node *root,int x)
{
int level=0;
node *temp;
queue<node*>q;
q.enqueue(root,level);
while(!q.empty())
{
temp=q.dequeue(); //temp is an arrow which contains node addr,level
// i am assuming the appropriate typecasts are done in the code
if(temp[0]->data==x)
return temp[1];
if(temp[0]->left)
q.enqueue(temp[0]->left,++temp[1]);
if(temp[0]->right)
q.enqueue(temp[0]->right,++temp[1]);
}
//if control reaches till here return -1
return -1;
}
1.) You are assuming you have already performed the BFS once and have all parent links
2.) In the code section
if (--count == 0)
{
level--;
count = q.size();
}
if the tree has elements
1
/\
2 3
/ \ / \
4 5 6 7
Then the --count==0 condition will only be met for the first iteration
cause the queue will progress like this
1 (condition will only be true here)
23
345
4567
null
Thus level will not get decremented always
This can be answered by a very simple modification to BFS
Create a Q struct as
struct Q{
node x;
int level;
struct Q *next;
}
BFS(node)
{
Enq will do as below
{Q.x=node
Q.lvl=1}
while(Q is not empty)
{
tmp node=Q.top.x
tmp lvl=Q.top.level;
deq(Q)
if(node->left and right !=elem)
Enq(node.l,lvl+1)
Enq(node.r,lvl+1)
else
global lvllock=lvl+1;
break;
}
}
Q will now contain values which will give us the cousins
for each value in Q seek till lvl+i is less than or equal to lvllock and then
print the values as its cousins til Q is empty
Repjoyjerraj, Android test engineer at ASAPInfosystemsPvtLtd
I am a sales manager .I'm responsible for leading and coaching a team of salespeople. A sales manager's ...
RepLisaTQuinn, Analyst at A9
I have a passion for reading and creative writing and attend many workshops and conventions surrounding love spell that work ...
RepAvyuktBurk, photographer at Precious Moments
Confident and dedicated photographer with experience in both professional and freelance photography. I like to explore Black Magic Spell To ...
Repirmajrichh, Junior programmer at AppNexus
I am Irma, seeking the position of a Historical Researcher at Littler’s where my expertise can be utilized to ...
Repmarygaustria, Analyst at ADP
I am Mary from Los Angeles, USA. I am working as a Manager in Fragrant Flower Lawn Services company. I ...
Repruchikadolph, HR Executive at Accenture
I choose the talking books to match the book library clerk blind library patrons . I compare a list of borrowers ...
RepLauraJOles, Android Engineer at A9
Hi i am Laura.I live in Us from my childhood and i love my country.My father he is ...
Repalicesreedg, Accountant at ADP
Hi my name is Alice and i am working in an IT company. I am working here from last 5 ...
Repjacksssones433, Apple Phone Number available 24/7 for our Customers at A9
Proficient and creative WordPress developer with a strong history in website management and development. Experienced in SEO and PPC campaign ...
RepGlennPCannon, Applications Developer at Techlogix
Hi everyone, I am a professor in Houston, USA. I like to explore new things about Hire Someone To Break ...
Repnorahuerta1111, Accountant at ABC TECH SUPPORT
Hey, I'm Nora Huerta, and I'm working as a journalist. And nowadays I am doing part time jobs ...
Repaaronlustera, Android test engineer at Absolute Softech Ltd
My job is to help groups of people work together better, understand their common goals, and plan to achieve those ...
RepRobin has more than 26 years of experience working for state, city, university, and public entities as a fisheries ecologist ...
think more..its bfs..u will be incrementing level even for same level nodes
- vsohail09 January 11, 2014