How to solve the problem forgetten tree.

My approach was recursively solve the problem:

```
solve(l,r,p):
for i=l to r:
if l to r in subtree of i:
i is the root of subtree(l,r) and p is the parent of i
solve for(l,i-1) and (i+1,r)
```

It passed first two subtasks.What is the correct way to solve the problem