TLE in QTREE6

can anyone tell me why my code is giving TLE. Thankyou in advance…
link to solution CodeChef: Practical coding for everyone
problem link QTREE6 Problem - CodeChef

I have changed my solution but getting WA this time…though i hv checked my program nearly for all testcases…can anyone tell me where my code is failing…Thankyou…solution link CodeChef: Practical coding for everyone

We would suggest you to go through the editorial of QTREE6 here: QTREE6 - Editorial - editorial - CodeChef Discuss and post your doubts in the comment section. This will help you get over your doubt fast.