C
C#2y ago
morry329#

✅ Case1 Failed: LeetCode Binary Tree Level Order Traversal

So I am studying some solutions from this puzzle https://leetcode.com/problems/binary-tree-level-order-traversal/solutions/?envType=study-plan&id=level-1&languageTags=csharp My current code is like this
public class Solution {
public IList<IList<int>> LevelOrder(TreeNode root) {
List<IList<int>> res = new();
List<int> separation = new();
if(root == null){
return res;
}
Queue<TreeNode> q = new Queue<TreeNode>();
q.Enqueue(root);
int levelCount = 1;
while(q.Count > 0){
TreeNode visited = q.Dequeue();

if(visited.left != null){
q.Enqueue(visited.right);

}

if(visited.right != null){

q.Enqueue(visited.left);
}
separation.Add(visited.val);
levelCount--;
if(levelCount == 0){
res.Add(separation);
levelCount = q.Count;

}
}
return res;


}
}
public class Solution {
public IList<IList<int>> LevelOrder(TreeNode root) {
List<IList<int>> res = new();
List<int> separation = new();
if(root == null){
return res;
}
Queue<TreeNode> q = new Queue<TreeNode>();
q.Enqueue(root);
int levelCount = 1;
while(q.Count > 0){
TreeNode visited = q.Dequeue();

if(visited.left != null){
q.Enqueue(visited.right);

}

if(visited.right != null){

q.Enqueue(visited.left);
}
separation.Add(visited.val);
levelCount--;
if(levelCount == 0){
res.Add(separation);
levelCount = q.Count;

}
}
return res;


}
}
But I get the following incorrect result (see the attached screenshot) Could anyone kindly tell me what my code has done 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.
3 Replies
Andri.G
Andri.G2y ago
try your code in other .net playground like https://dotnetfiddle.net/. there is a lot case where my code give wrong result in leetcode but correct in the playground. maybe leetcode using old/different compiler i dont know. i give up using leetcode because of that
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
morry329#
morry329#OP2y ago
Thanks for the suggestion 🙂 I will make more use of dotnetfiddle
Want results from more Discord servers?
Add your server