Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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# 将二叉树返回到变量_C#_Return_Binary Tree - Fatal编程技术网

C# 将二叉树返回到变量

C# 将二叉树返回到变量,c#,return,binary-tree,C#,Return,Binary Tree,我有一个练习,我正在使用二叉树,其中有两个函数可以使用,它们如下所示: class Program { public Node root; static void Main(string[] args) { //build and return tree var b = binaryTree(); //get sum of tree var x = binaryTreeSum(b); //C

我有一个练习,我正在使用二叉树,其中有两个函数可以使用,它们如下所示:

class Program
{
    public Node root;
    static void Main(string[] args)
    {
        //build and return tree
        var b = binaryTree();

        //get sum of tree
        var x = binaryTreeSum(b);
        //Console.WriteLine("Sum of the binary tree is " + x);

    }
我在
var b=binaryTreeSum(b)
非静态字段、方法或属性需要对象引用时出错。我试图用以下函数创建我的树:

    public Program binaryTree()
    {
        //build tree
        Program tree = new Program();

        tree.root = new Node(10);
        tree.root.left = new Node(5);
        tree.root.right = new Node(11);
        tree.root.left.left = new Node(2);
        tree.root.left.right = new Node(1);
        tree.root.right.left = new Node(5);
        tree.root.right.right = new Node(123);

       return tree;
    }

有没有办法将我的
树.根
返回到
var b
?我知道我可以使用
tree.root
在下一个函数中获得总和,这正是我要做的。我可能一开始就搞错了,但我必须使用这两个函数并返回到
var
我想你只是想
返回树
根据方法签名,您希望返回一个
BinaryTree
对象,但当前正试图返回一个
节点

由于我不确定这些对象来自何处(您创建了它们?),我假设其中一个对象不是从另一个对象派生的


binaryTree()
函数的返回值将自动分配给
var a

您的问题不清楚。你已经在归还那棵树了。您的代码从来没有定义过“a”,只有“b”(var b=binaryTree())。你有错误吗?你看到的是什么行为而不是你所期望的?
Program
对于一个表示二叉树的类来说,听起来是个很糟糕的名字。我建议改为使用类似于
BinaryTree
的东西。