vector inorder(Node* root,vector vect){ if(root==NULL){ return vect; } inorder(root->left,vect); vect.push_back(root->data); inorder(root->right,vect); return vect; } vector printCommon(Node *root1, Node *root2) { vector vect1; vector vect2; vector res; if(root1==NULL || root2==NULL){ return res; } inorder(root1,vect1); inorder(root2,vect2); int n,m; n=vect1.size(); m=vect2.size(); int i=0,j=0; while(i<n || j<m ){ if(vect1[i]<vect2[j]){ i++; } if(vect1[i]>vect2[j]){ j++; } else{ res.push_back(vect1[i]); i++;j++; } } return res; }
Ques:-Print common nodes in BST's .PLease tell me whats wrong in this as it is printing null every time
Please share your code in Coding blocks ide
Actually i am not able to copy paste there so thats why i sahre my code here can you please tell another way
Share via gfg Ide
or Press the button above tab on your keyboard three times ``` enter space and then paste your code
Ques:- Print common nodes between two bstβs
bro properly indent it and then share
Hey @manikjindal49
void inorder(Node* root,vector<int> &vect){ //correction 1 :Pass by reference otherwise changes won't reflect
if(root==NULL){
return ;
}
inorder(root->left,vect);
vect.push_back(root->data);
inorder(root->right,vect);
return ;
}
while(i<n || j<m ){
if(vect1[i]<vect2[j]){
i++;
}
else if(vect1[i]>vect2[j]){ //correction 2:here add else if instead of if
j++;
}
else{
res.push_back(vect1[i]);
i++;j++;
}
}
Yes i understand my mistake thanks
What happened ???
But it is failing this test case tree1:-11 2 31 1 7 17 46 N N 4 9 13 tree2:-48 9 50 7 28 49 N 6 8 18 33
I think this input is incorrect
When you will form the tree ,you will find that its not a BST
Print its inorder only and check
But it is a question of geek for geeks and they have given the question can you please check it again with my example
Bro I checked on their portal only
Print the inorder of the trees on gfg platform and see if its sorted or not
It should be sorted if BST right ,but its not sorted so there must be some problem in the test cases