首页 > 解决方案 > 编译错误:“Program.Node”不包含采用 3 个参数的构造函数

问题描述

我正在编写一个测试程序来测试给定的二叉树是否为 BST,但我遇到以下错误:

Compilation error (line 33, col 13): 'Program.Node' does not contain a constructor that takes 3 arguments
Compilation error (line 34, col 13): 'Program.Node' does not contain a constructor that takes 3 arguments
Compilation error (line 35, col 13): 'Program.Node' does not contain a constructor that takes 3 arguments

using System;

public class Program
{
    public  class Node
    {
        public int value;
        public Node left;
        public Node right;      
    }



     public static bool IsValidBST(Node root) {
        return IsValid (root, int.MinValue, int.MaxValue);

    }

    private static bool IsValid (Node node, int Min, int Max)
    {
        if (node == null)
            return true;

        if((node.value > Min && node.value < Max) && IsValid (node.left , Min, node.value) && IsValid(node.right, node.value, Max))
            return true;
        else
            return false;
    }

    public static void Main()
    {
        Node n1 = new Node(1,null,null);
        Node n3 = new Node(3,null,null);
        Node n2 = new Node(2, n1, n3);
        Console.WriteLine(IsValidBST(n2));
        Console.ReadLine();

    }
}

标签: c#

解决方案


您需要将构造函数添加到Node类定义中。

public class Node
{
    public int value;
    public Node left;
    public Node right;

    public Node(int value, Node left, Node right)
    {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}

推荐阅读