Please tell me how to approach this question?

give me some hint how to approach this ques

You can think of applying kadane’s algorithm on trees. Basically root the tree at 1, and then find the best cost you can get to reach a node i. Now in second dfs see the best cost if you start at node i, and go in one of the subtrees of i, and not towards the parent. Push all these values in vector corresponding to that node. Now you can choose the two best among these. See this code for reference https://ide.codingblocks.com/s/577846

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.