in Programming in C
1,526 views
1 vote
1 vote

Consider the following function in a single linked list
int fun(struct node *P, struct node *Q)
{
if(P==NULL && Q == NULL) 
return 0;
else if(P==NULL || Q==NULL)
return 1;
else if(P→data!=Q→data)
return 1;
if(fun(P→ left, Q→ left) || fun(P→right, Q→right))
return 1;
else return 0;
}

Assume node is a structure types with three members as follows :
Struct node{
int data;
struct node *left;
struct node *right;
}
If two binary tree root pointers are passed to the function f(), then which of the following statements is/are correct( Marks: 0.00 )

  1.  It compares two given binary trees and returns 0 if two trees are different and it returns 0 other wise.
  2.   It compares two given binary trees and returns 1 if two trees are different and it returns 0 other wise.
    Explanation:
    It returns 1, when both trees are different.
    It returns 0, when both trees are same recursively.
  3.   It compares two given binary trees but return value cannot be used to differentiate the trees
  4.   None of these
in Programming in C
1.5k views

2 Answers

1 vote
1 vote

May be this works ....

Plz make 2 small one equal and one unequal tree .....

0 votes
0 votes

it compares two given trees and return '0' two trees are same

else return '1' two trees are different.

execute 'fun' function-----if both tree roots are null then it returns '0'(means two are same)

-----------------------------------if one tree root is null and other is not it returns '1'(means two are different)

------------------------------else compare left subtree and right subtree(there is or condition between left and right subtree.if left subtree value is not equal to left subtree of another tree it returns '1'.and no need to compare right subtree because or operation is always returns '1')

so ans is B

Related questions