The Gateway to Computer Science Excellence
+1 vote
86 views

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 by Active (2.7k points) | 86 views

2 Answers

+1 vote

May be this works ....

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

by Junior (539 points)
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

by Boss (12.8k points)
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,497 answers
195,489 comments
100,804 users