I understood why the test cases are failing but can anyone tell how to print those two numbers?
I tried several times but couldn’t do it.
Recover Binary Search Tree
if((root->data) < ((root->left)->data)){
cout << root->data << " " << (root->left)->data;
return;
}
else if(root->data >= ((root->right)->data)){
cout << root->data << " " << (root->right)->data;
return;
}
in these statements, if either of root’s right or root’s left is NULL then accessing it’s data is a fault, so that is the error
I already have this part in my code that if root->left or right != NULL, then only it will access those elements. My question was how do we print those two numbers.
I know a way but I don’t know how to implement it, one is take inorder traversal of the tree in a array and see if the data of a node is less than the previous node, store the previous node, And the another one number which is less than the previous number, store the current node.
If the numbers are adjacent than print both the previous and the current node.
yes, your approach will work, however i am giving below a better solution (only implemented function)
this actually fixes the entire tree, look only how to find first and second
works on similar lines, but just stores those two values and not the entire tree as an array
It’s done.