Here's my function signature:
public IList<IList<int>> LevelOrder(TreeNode root)
Here's the snippet showing how I create the IList<IList<int>>
that I return from the function:
IList<IList<int>> result = new List<List<int>>();
result.Add(new List<int>());
while (completeQueue.Count != 0)
{
current = completeQueue.Dequeue();
if (level != current.level)
{
result.Add(new List<int>());
level = current.level;
}
result[level].Add(current.node.val);
}
return result;
When I run this function I get a error from the final line (return result
):
Line 49: Char 36: error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.List<int>>' to 'System.Collections.Generic.IList<System.Collections.Generic.IList<int>>'. An explicit conversion exists (are you missing a cast?) (in Solution.cs)
List
implements the IList
interface. Why doesn't this work?
>` instead. Given that, the problem is exactly as described in the marked duplicate.
>();` Indeed, if the actual code were as the OP claims, the error message would occur on that variable declaration, not the `return` statement as they claim. ...