Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples: 1 / \ 2 3 / \ / \ 4 5 6 7 The tree has 5 vertical lines Vertical-Line-1 has only one node 4 => vertical sum is 4 Vertical-Line-2: has only one node 2=> vertical sum is 2 Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 Vertical-Line-4: has only one node 3 => vertical sum is 3 Vertical-Line-5: has only one node 7 => vertical sum is 7 So expected output is 4, 2, 12, 3 and 7
assuming the number is of 4 bits only..
ReplyDeleteint oddbitsmask=0x0101;
int evenbitsmsk=0x1010;
int ans=((number & oddbitmask)<<1) &((number&evenbitmask)<<1);
@ankit continuing with same logic for a number with 8 bits
ReplyDeletemask will be
int oddbitsmask=0xAA;
int evenbitsmsk=0x55
it should be like this:-
ReplyDeleteans=( ( n & oddbitmask ) << 1 ) | ( ( n & evenbitmask ) >> 1 );
@ankit please correct your answer...
ReplyDelete@priyaranjan:
ReplyDeleteI always encounter problems in bitwise operator.can u please give a link from where i can understand these...
also is it neccessary that this question can be done only using bitwise operator..
according to me,
int swap(int a[])
{
for(i=0;i<=3;i++)
{
swap(a[2*i],a[(2*i)+1]);
}
return a;
}
@sorry...
ReplyDeletethe above code be lyk dis..
int interchange(int a[])
{
for(i=0;i<=3;i++)
{
swap(a[2*i],a[(2*i)+1]);
}
return a;
}
@kamakshi no it is not mandatory to do it using bitwise but as you must be knowing there is no intrinsic support of BOOL value in c,so we have to operate on individual bits.
ReplyDeleteIn your code you are taking an integer array,so it is not efficient.
Try to solve many problems related to bitwise.