I think the solution given in the editorial will occupy a lot of space. Can you see my code and tell whether it is more efficient than the editorial one?

I also first thought of same solution given in the editorial but it will occupy more space as we are maintaining a vector or array at every node to store the indices and it can occupy a lot of space.

Please Check my code: Simple Solution

but this also trades off in time, the editorial solution will do it in lesser time, plus in worst case, space will also almost be same