我需要写的操作,算谁有两个儿子是彼此相等的节点。我试过,但我得到错误,并不是所有的代码路径都返回一个值。 请帮我做个测试 谢谢。计数节点
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();
}
它不是为我工作 –
你能指出新的错误? – 0xDEFACED
我编辑的问题,它返回它的0(见主) –