c# - 编译错误:“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();
}
}
解决方案
您需要将构造函数添加到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;
}
}
推荐阅读
- python - 如何模拟受新闻影响的股价
- blockchain - 为什么我在读取以太坊账户余额时会得到两个不同的答案?
- c - 使用 scanf() 和 printf() 不会打印 char 并忽略 \n:
- javascript - 如何使它自动显示没有按钮的文本?
- reactjs - 使用 apollo 客户端 fetchMore Apollo Client 多次触发相同的查询并附加数据
- google-bigquery - 在 BQ 中运行依赖的计划查询
- javascript - 具有不同 props 的相同组件的条件渲染不会在 ReactJS 中卸载组件
- react-query - react-query - useQuery 和 useMutation 钩子有什么区别?
- r - R:如何将向量扩展为更大的长度以保持其唯一值?
- c++ - 使用后端列表对 QAbstractListModel 进行排序