C
C#17mo ago
morry329#

✅ Cannot implicit convert type

I am working on this LC puzzle https://leetcode.com/problems/binary-tree-level-order-traversal/?envType=study-plan&id=level-1 Here's my progress:
public IList<IList<int>> LevelOrder(TreeNode root) {
List<int> result = new List<int>();
if(root == null){
return result;
}

Queue<TreeNode> queue = new LinkedList<TreeNode>(); //ERROR HERE
queue.Enqueue(root);
while(queue != null){
int size = queue.Count();
List<int> curr = new List<int>();
for(int i = 0; i < size; i++){
TreeNode node = queue.Dequeue();
curr.Add(node.val);
}
curr = result;
}
return result;
}
public IList<IList<int>> LevelOrder(TreeNode root) {
List<int> result = new List<int>();
if(root == null){
return result;
}

Queue<TreeNode> queue = new LinkedList<TreeNode>(); //ERROR HERE
queue.Enqueue(root);
while(queue != null){
int size = queue.Count();
List<int> curr = new List<int>();
for(int i = 0; i < size; i++){
TreeNode node = queue.Dequeue();
curr.Add(node.val);
}
curr = result;
}
return result;
}
` I am getting the error Cannot implicitly convert type 'System.Collections.Generic.LinkedList<TreeNode>' to 'System.Collections.Generic.Queue<TreeNode>' (see the ERROR HERE in the code). Could anyone kindly tell me why?
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
Angius
Angius17mo ago
Well, the error is quite obvious You're trying to treat a chair like it was an apple You wouldn't do int thing = new List<string>(), would you?
morry329#
morry329#17mo ago
No, I would not do that 🙂 Ok I just need to cool down my head haha Thanks for pointing out anyway 🙂
Want results from more Discord servers?
Add your server
More Posts