Please tell me where i am doing mistake in this code?
consider this example
here 65 should return (2,true) because 65 and 50 forms a BST
you didn’t handle this case
i have already included the case when left and right tree are BST and root is also satisfying the property of a BST node
can you please edit the code and explain the code with the help of comments
Modified Code
mistakes
- add these two conditions
// if max sub tree is left part + root node??
if(lt.isBST && root->data > lt.max&& root->right==NULL){
result.sz=lt.sz+1;
result.isBST=true;
result.max=max(lt.max,root->data);
result.min=min(lt.min,root->data);
return result;
}
// if max sub tree is right part + root node??
else if(rt.isBST&& root->data < rt.min&&root->left==NULL){
result.sz=rt.sz+1;
result.isBST=true;
result.max=max(rt.max,root->data);
result.min=min(rt.min,root->data);
return result;
}
- in the first if condition correct size will be calculated as
result.sz = lt.sz+rt.sz + 1 ;
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.