【发布时间】:2010-11-08 19:50:25
【问题描述】:
用这种方法计算一棵树上的孙子数量,而不是曾孙的数量有什么问题?
(child1 是左孩子,child2 是右孩子) 另外,这个方法不应该带任何参数。如果您提供替代解决方案,还请告诉我我当前的解决方案有什么问题..
public int countGrandChildren() // but not greatGrandChildren
{
int count=0;
int depth=1;
if (depth<4){
if (child1!=null){
count+=child1.countGrandChildren();
depth++;
if (depth==3)
count++;
}
if (child2!=null){
count+=child2.countGrandChildren();
depth++;
if (depth==3)
count++;
}
}
return count;
}
【问题讨论】:
-
请不要创建与您的旧问题基本相同的新问题...
-
没有人帮我解决另一个问题
-
上一个问题中给出了所有可能(合理)的答案。再次提出问题不会改变这一事实!