Don’t know what is my mistake
I am taking each node of a tree and checking the number of nodes satisfying the property of BST considering the node as the root and returning the maximum answer. Even giving me the test case would probably help me in debugging my code. I have tested for several test case but not able to figure out where I am going wrong.
One test case failing in largest BST in a BT
Simply checking each node for the property of bst and increasing the counter is the wrong approach.
Try this method -
The inorder traversal of a BST is always in ascending order. So we can find the size of the maximum subarray possible which will be in ascending order. And that will be our answer.
Are you sure that every subarray of the inorder is a subtree of the tree? I don’t think so. Can you please clearify.
The inorder traversal is given to you. Now just take all subarrays. Take those in consideration which are in ascending order. And finally return the maximum size among all subarrays taken into account.
Yes every subarray is a subtree. You can try it out.
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.