Need help - Tree Distances (TDISTS)

I have tried to solve this problem and I find my logic correct.
But I can’t get past TLE.
Can someone find the error in below written code?
Submitted Code

Logic is written in code itself.

Try using “\n” instead of endl.


It works on changing to “\n”.(Thanks )

I don’t get it. How are they different?

‘endl’ flushes the output after every use.
Using ‘\n’ allows the program to store output in buffer, and flush it to stdout in an optimised manner.

Using ‘endl’ is good for only interactive problems where you want output to be flushed everytime.


They are different because endl flushes the output whereas ‘\n’ doesnt. Flushing takes time and hence ‘\n’ is faster. But in interactive problems you have to use endl because flushing is necessary

1 Like