Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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# - Fatal编程技术网

C# 二叉搜索树验证

C# 二叉搜索树验证,c#,C#,谢谢你阅读我的主题。我正在用C#at测试圆顶进行一点练习。我目前正在尝试编程的练习如下: 我当前的结果: 示例:正确答案 简单案例:正确答案 常规案例:错误答案 边缘案例:错误答案 性能测试:回答错误 您可以阅读下面我编写的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using Sy

谢谢你阅读我的主题。我正在用C#at测试圆顶进行一点练习。我目前正在尝试编程的练习如下:

我当前的结果:

  • 示例:正确答案
  • 简单案例:正确答案
  • 常规案例:错误答案
  • 边缘案例:错误答案
  • 性能测试:回答错误
您可以阅读下面我编写的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class Node
{
    public int Value { get; set; }                      // Value of {node} == integer (1,2,3,4,5)
    public Node Left { get; set; }                      // Reference to left node (n1, n2, n3) == Node.
    public Node Right { get; set; }                     // Reference to right node (n1, n2, n3) == Node.
    public Node(int value, Node left, Node right)       
    {
        Value = value;                                  // (int)
        Left = left;                                    // (node)
        Right = right;                                  // (node)
    }
}

public class BinarySearchTree
{
    public static bool IsValidBST(Node root)
    {
        if (root.Left == null && root.Right == null)
            return true;

        else if (root.Left == null)
        {
            // Checking root.Right.Value
            if (root.Value <= root.Right.Value)
                return true;

            else
                return false;
        }
        else if (root.Right == null)
        {
            // Checking root.Left.Value
            if (root.Value > root.Left.Value)
                return true;
            else
                return false; 
        }
        else
        {
            // Checking both Values
            if (root.Value > root.Left.Value && root.Value <= root.Right.Value)
                return true;
            else
                return false;
        }
    }


    public static void Main(string[] args)
    {
        Node n1 = new Node(1, null, null);
        Node n3 = new Node(3, null, null);
        Node n2 = new Node(2, n1, n3);

        // Execute function and write it to the console
        Console.WriteLine(IsValidBST(n2));
        Console.ReadLine();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
使用System.Threading.Tasks;
公共类节点
{
公共int值{get;set;}//{node}的值==整数(1,2,3,4,5)
公共节点Left{get;set;}//对左节点的引用(n1,n2,n3)=节点。
公共节点Right{get;set;}//对右节点的引用(n1,n2,n3)=节点。
公共节点(int值、左节点、右节点)
{
Value=Value;//(int)
左=左;//(节点)
右=右;/(节点)
}
}
公共类二进制搜索树
{
公共静态bool isvalidbtst(节点根)
{
if(root.Left==null&&root.Right==null)
返回true;
else if(root.Left==null)
{
//正在检查root.Right.Value
if(root.Value root.Left.Value)
返回true;
其他的
返回false;
}
其他的
{
//检查两个值

如果(root.Value>root.Left.Value&&root.Value您没有检查所有上层节点的值。您应该具有所有上层节点的数组/列表,以便可以检查它

还应该使用递归调用。 像这样

IsValidBST(Node root, List<int> upperValues = null) 
{
 ... 
 bool childIsValid = IsValidBST (root.Left, upperValuesLeft ) && IsValidBST(root.Right, upperValuesRight )
}
isvalidbtst(节点根,列表上限值=null)
{
... 
bool childIsValid=IsValidBST(root.Left,upperValuesLeft)和&IsValidBST(root.Right,upperValuesRight)
}

您没有检查所有上层节点的值。您应该有所有上层节点的数组/列表,以便检查它

还应该使用递归调用。 像这样

IsValidBST(Node root, List<int> upperValues = null) 
{
 ... 
 bool childIsValid = IsValidBST (root.Left, upperValuesLeft ) && IsValidBST(root.Right, upperValuesRight )
}
isvalidbtst(节点根,列表上限值=null)
{
... 
bool childIsValid=IsValidBST(root.Left,upperValuesLeft)和&IsValidBST(root.Right,upperValuesRight)
}

您应该通过调用
isvalidbtst
检查那些其他节点
root.Left
root.Right
,如果它们不为
null
,然后检查这些结果。现在您只测试根节点,而不是下降到树中。您应该检查那些其他节点
root.Left
root.Right
如果它们不是
null,则对它们调用
isvalidbtst
,然后检查这些结果。现在您只测试根节点,而不是下降到树中

答案代码:

if (root.Value == value) return true;

if (root.Left != null && value < root.Value)
    if (Contains(root.Left, value)) return true;

if (root.Right != null && value > root.Value)
    if (Contains(root.Right, value)) return true;

return false;
if(root.Value==Value)返回true;
if(root.Left!=null&&valueroot.value)
if(Contains(root.Right,value))返回true;
返回false;
阅读相同的问题。

答案代码:

if (root.Value == value) return true;

if (root.Left != null && value < root.Value)
    if (Contains(root.Left, value)) return true;

if (root.Right != null && value > root.Value)
    if (Contains(root.Right, value)) return true;

return false;
if(root.Value==Value)返回true;
if(root.Left!=null&&valueroot.value)
if(Contains(root.Right,value))返回true;
返回false;

阅读相同的问题。

非常感谢!:-)非常感谢!:-)非常感谢!:-)非常感谢!:-)在这里查看我的答案:在这里查看我的答案: