Replace with the sum of greater nodes

i tried writing this code and I am still not able to figure out why it is showing TLE error. please check

agreed I am sorting the array but if i print my root after building the tree. its the right tree as shown in explanations. so why my replace is still isnt working correctly.

Your approach is not correct

  1. you build Tree function in not according to question
  2. Replace function is also not correct

check this approach

i have commented each steps

if you have still doubts feel free to ask

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.