538 views
2 votes
2 votes

Let $B$ be a rooted binary tree of $n$ nodes. Two nodes of $B$ are said to be a sibling pair if they are the children of the same parent. For example, given the binary tree in Figure 1, the sibling pairs are (2, 3) and (6, 7). Design an $O(n)$ time algorithm that prints all the sibling pairs of $B$.

1 Answer

2 votes
2 votes
int siblingpairs( Struct node *root ){

if(root==Null)

return  0;

if(root → lc==Null || root →rc==Null)

{
return 0;

}

if(root → lc==Null && root →rc==Null)

{
return 0;

}

else{

LS= siblingpairs(root→lc);

RS=siblingpairs(root→rc);

return (root→lc,root→rc);

}

}
edited by

Related questions

14 votes
14 votes
2 answers
1
go_editor asked May 31, 2016
1,662 views
Let $H_1$ and $H_2$ be two complete binary trees that are heaps as well. Assume $H_1$ and $H_2$ are max-heaps, each of size $n$. Design and analyze an efficient algorithm...
1 votes
1 votes
1 answer
3
go_editor asked May 31, 2016
650 views
Consider a uniprocessor system with four processes having the following arrival and burst times:$$\begin{array}{|c|c|c|l|} \hline&\text{Arrival Time}&\text{CPU Burst Time...