Largest BST in a bt

https://ide.codingblocks.com/s/106292 Please check. its working on offline compilers. I can not understand why the program gets terminated after printing the node 50.

@samriddhi.srivastava2017
The problem is with your code is with the preorder function. You have given it int return type and yet the only return statement in it is if head is NULL. In any other case , your code doesn’t return anything. Since you are computing your final answer using it , it will always be wrong. Answer computed by successive recursive calls is not passed or used by parent calls and hence goes to waste. Since there is not return statement at the end of function , it will return any random value back to parent call and ruin your answer.

https://ide.codingblocks.com/s/106703 Please check if I have made the correct changes

@samriddhi.srivastava2017
Your code requires a few modifications. First of all , the problem requires you to print the no of nodes in the largest BST , not its height. You should use this count function and call it in Line No. 58 instead of ht function

long count(node*root){
if(root==NULL){
return 0;
}
return 1 + count(root->left) + count(root->right);
}

Also , your code does not make any recursive calls on the right subtree. Since you have written a return statement at Line No. 65 , your program evaluates the left part and simply returns the answer obtained from the left part only and never bothers to check the right part. No code is run after a return statement. Instead modify them like this -

int leftAns = preorder(head->left,tt);
int rightAns = preorder(head->right,tt);
return max(rightAns,leftAns) ;

Make these changes and try again. Let me know if you face any further problems.

https://ide.codingblocks.com/s/107152 . Sir, its still not showing the data for the last node

@samriddhi.srivastava2017
I submitted your code after commenting out the debugging statements and got a full score. I again tried your code with the debugging after moving the statements
cout<<“head”<< head->data<<endl;
cout<<“maxv”<<maxv<<endl;
below the if(head == NULL) statement since it gives a runtime error if head->data is tried to access when head is NULL.
The code perfectly as expected and covered all nodes.
Could you please describe the issue you are facing in more detail ?
I really think your code is working fine as it should.

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.