✅ A LeetCode glitch or am I wrong with my code?
So again this is the LC puzzle https://leetcode.com/problems/binary-tree-level-order-traversal/description/
My solution is as follows;
But one line of code bothers me still. This one
int count = q.Count;`
If I do like this instead of initialising the integer count:
`
I get this result
`
Could anyone kindly tell me it is a glitch on LC or am I wrong? (if I am wrong I appreciate it if you could point out which part of my code is wrong)LeetCode
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
2 Replies
that evaluation (
i < q.Count
) runs every iteration
so if you enqueue/dequeue stuff in q, you will end up with a different count every few iterations
meanwhile, doing int count = q.Count;
before the loop ensures its value doesn't change while you're iteratingAhhhhhh I see! Thank you so much for pointing me that out 🙏 I see the error more clearly now 🔮