Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当尝试将'Concat'与'as'一起使用时,如何理解这个'System.ArgumentNullException'`_C#_Data Structures_Casting_Binary Tree - Fatal编程技术网

C# 当尝试将'Concat'与'as'一起使用时,如何理解这个'System.ArgumentNullException'`

C# 当尝试将'Concat'与'as'一起使用时,如何理解这个'System.ArgumentNullException'`,c#,data-structures,casting,binary-tree,C#,Data Structures,Casting,Binary Tree,我试图解决一个Leetcode问题,二叉树的预序遍历。以下是我到目前为止写的内容: using System.Collections.Generic; using System.Linq; public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; } } public class Solut

我试图解决一个Leetcode问题,二叉树的预序遍历。以下是我到目前为止写的内容:

using System.Collections.Generic;
using System.Linq;

public class TreeNode
{
   public int val;
   public TreeNode left;
   public TreeNode right;
   public TreeNode(int x) { val = x; }
}

public class Solution
{
   public IList<int> PreorderTraversal(TreeNode root)
   {
        if (root == null)
        {
            return new List<int> { };
        }
        else
        {
            IList<int> ret = new List<int> { root.val };
            ret = ret.Concat(PreorderTraversal(root.left)) as IList<int>;
            ret = ret.Concat(PreorderTraversal(root.right)) as IList<int>;// <-- this line
            return ret;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        var root = new TreeNode(42);
        root.right = new TreeNode(99);
        var result = new Solution().PreorderTraversal(root);
    }
}
使用System.Collections.Generic;
使用System.Linq;
公共级树节点
{
公共国际价值;
公共树节点左;
公共树节点权;
公共树节点(intx){val=x;}
}
公共类解决方案
{
公共IList预订单Traversal(树节点根)
{
if(root==null)
{
返回新列表{};
}
其他的
{
IList ret=新列表{root.val};
ret=ret.Concat(PreorderTraversal(root.left))作为IList;

ret=ret.Concat(PreorderTraversal(root.right))作为IList;//虽然
ret
的初始值是
IList
,但使用
.Concat
时的返回类型是
IEnumerable
。但是您使用的是保险柜(
as
)强制转换回一个
IList
;这不是一个有效的强制转换,因此您的第一个
.Concat
操作返回null…然后下一行尝试对null值进行操作

.Concat
实现为一个扩展方法,该方法将源(
ret
在本例中)作为参数,因此当传递null时,它会抛出一个
ArgumentNullException

您可以使用
IEnumerable
作为返回类型,声明
ret
并删除强制类型转换来修复此问题

using System.Collections.Generic;
using System.Linq;

public class TreeNode
{
   public int val;
   public TreeNode left;
   public TreeNode right;
   public TreeNode(int x) { val = x; }
}

public class Solution
{
   public IEnumerable<int> PreorderTraversal(TreeNode root)
   {
        if (root == null)
        {
            return Enumerable.Empty<int>();
        }
        else
        {
            IEnumerable<int> ret = new List<int> { root.val };
            ret = ret.Concat(PreorderTraversal(root.left));
            ret = ret.Concat(PreorderTraversal(root.right));
            return ret;
        }
    }
}

 class Program
{
    static void Main(string[] args)
    {
        var root = new TreeNode(42);
        root.right = new TreeNode(99);
        var result = new Solution().PreorderTraversal(root);
    }
}
使用System.Collections.Generic;
使用System.Linq;
公共级树节点
{
公共国际价值;
公共树节点左;
公共树节点权;
公共树节点(intx){val=x;}
}
公共类解决方案
{
公共IEnumerable预订单Traversal(TreeNode根)
{
if(root==null)
{
返回可枚举的.Empty();
}
其他的
{
IEnumerable ret=新列表{root.val};
ret=ret.Concat(PreorderTraversal(root.left));
ret=ret.Concat(PreorderTraversal(root.right));
返回ret;
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
var root=新的树节点(42);
root.right=新的树节点(99);
var result=new Solution().PreorderTraversal(根);
}
}