1,523 views
1 votes
1 votes

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

2 Answers

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

0 votes
0 votes
1 answer
1
Mrityudoot asked Feb 25
144 views
How can we find the highest element in a singly linked list in O(1)? We are free to use any extra space.
9 votes
9 votes
2 answers
4
GO Classes asked May 4, 2022
483 views
If $f(n) = O(g(n))$ and $f(n) = \Omega(g(n)),$ then it is always true that$f(n) = o(g(n)).$$f(n) = \theta(g(n)).$$f(n) = \omega(g(n)).$both A and B are always true.