in this question it is mentioned that node should replace by sum of element greater then or equal but in my approach i am only replacing it by sum of greater + its value all test cases passed i want to know is my approach correct??
Replace by sum of nodes
If we have to replace it by greater then Or equal to then how should I proceed can you correct my code??
@shampblocks you are simply making calls to right and left subtree and adding the current val, to get the answer so you are not checking any conditions at all, therefore, this code will work for all values greater or equal. Simply the order in which you make the recursive calls takes care of everything.
Yes I know my approach will not work that’s why I asked to guide me in some other approach
It will work for the sample case given but it will not work for the case when I need to replace it by the sun of it’s smaller elements because I am not going to left first…
@shampblocks you dont need to add the SMALLER elements… all you need is greater or equal elements. By the way, a Binary Search Tree by definition has distinct keys and duplicates in binary search tree are not allowed. So you dont need to worry about same values either way.
Ohk thanks mam… …
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.