gpt4 book ai didi

c# - 计算有两个相同儿子的节点

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:40:25 26 4
gpt4 key购买 nike

我需要编写操作来计算具有两个彼此相等的儿子的节点。我试过了,但出现错误,并非所有代码路径都返回一个值。请帮我做个测试谢谢。

public static int CountWhoHasTwoSameSons(BinNode<int> Head)
{
if (Head != null)
{
if (IsLeaf(Head))
return 1;

if ((Head.HasLeft() && Head.HasRight()) && (Head.GetRight() == Head.GetLeft()))
return 1 + CountWhoHasTwoSameSons(Head.GetLeft()) + CountWhoHasTwoSameSons(Head.GetRight());
}

}

static void Main(string[] args)
{
BinNode<int> t = new BinNode<int>(3);
BinNode<int> t1 = new BinNode<int>(3);
BinNode<int> t2 = new BinNode<int>(3);
BinNode<int> t3 = new BinNode<int>(3);
BinNode<int> t4 = new BinNode<int>(t,3,t1);
BinNode<int> t5 = new BinNode<int>(t2,3,t3);
BinNode<int> t6 = new BinNode<int>(t4,3,null);
BinNode<int> Head = new BinNode<int>(t6,3,t5);
Console.WriteLine(SumTree(Head));
Console.WriteLine(LeafCounter(Head));
Console.WriteLine(CountWhoHasTwoSameSons(Head));

Console.ReadLine();
}

最佳答案

您需要在 If 语句外添加一个 return,编译器无法确定此函数是否会返回某些内容。如果您可以在返回 0 的函数末尾添加一个 return 语句,它应该可以工作。不是最喜欢的修复方法,您应该真正重写函数,这样返回实际上不仅仅是一种取悦编译器的方式,但它应该可以工作。

丹尼

关于c# - 计算有两个相同儿子的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43019896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com