provide me an approach to this prob
Please give me some hint
@kodivine0, here, approch is quite easy but the implementation might be tricky so,what you can do is for every node you can check if the subtree rooted as node is a BST or not if yes then return the size of that subtree and if not call the function for its left child and right child and return the max of two.
https://ide.codingblocks.com/s/331132 sir , i have tried to implement it the way u told me, but its not working
@kodivine0,you are in right direction its just that i am not getting the way you implemented the sum function .
you can refer below for the concept is explained in detail with code:-
naive approach :- https://ide.codingblocks.com/s/331575
optimised approach :- https://ide.codingblocks.com/s/331574
for the first case in sum function, there in no upper and lower boundary for the root, but as we subsequently traverse through left and right, there comes a restriction . for eg- if we traverse left, then there is no restriction for left boundary but for right boundary, it should be less than root. same can be done, if we traverse to right part of root.