Unable to convert List<List<int>> to return type IList<IList<int>>
Just change the declaration of your result to List<IList<int>>
.
List<T>
implements IList<T>
, but List<List<T>>
does not implement IList<IList<int>>
. Generic parameters are not covariant or contravariant unless defined that way and IList<T>
is not, so the type must match exactly.
public IList<IList<int>> LevelOrder(TreeNode root)
{
var result = new List<IList<int>>();
var que = new Queue<TreeNode>();
//if(root==null) return result;
que.Enqueue(root);
while (que.Count != 0)
{
int n = que.Count;
var subList = new List<int>();
for (int i = 0; i < n; i++)
{
if (que.Peek().left != null)
que.Enqueue(que.Peek().left);
if (que.Peek().right != null)
que.Enqueue(que.Peek().right);
subList.Add(que.Dequeue().val);
}
result.Add(subList);
}
return result;
}