i am given two bst trees
fill the gaps so it will return the number nodes which are equal in both trees
Code:
typedef struct Node{
int Val;
struct Node *Left, *Right;
} Node;
int CommonNodes(Node *T1, Node *T2)
{
if ( ?? 1 ?? || ?? 2 ?? )
return 0;
if ( ?? 3 ?? == ?? 4 ?? )
return ?? 5 ?? + ?? 6 ?? + ?? 7 ?? ;
if ( ?? 8 ?? )
return CommonNodes(?? 9 ??) + CommonNodes(?? 10 ??)
- CommonNodes(?? 11 ??) ;
if ( ?? 12 ?? )
return ?? 13 ?? + ?? 14 ?? - ?? 15 ?? ;
}
i tried like this
i know that its some how about
when we take the same common nodes twice
so we ned to subtract that group
but i cant see when it happens
Code:
int CommonNodes(Node *t1, Node *t2)
{
if (!t1||!t2 )
return 0;
if ( t1->val == t2->val)
return CommonNodes(t1->left,t2->left) + CommonNodes(t1->right,t2->right) +1;
if ( ?? 8 ?? )
return CommonNodes(?? 9 ??) + CommonNodes(?? 10 ??)
- CommonNodes(?? 11 ??) ;
if ( ?? 12 ?? )
return ?? 13 ?? + ?? 14 ?? - ?? 15 ?? ;
}