
0如果其中一个子代为,则返回代码
null。这是不正确的,因为您没有考虑另一个孩子或
this。该计数应始终是
>=1因为树中始终至少有一个节点。
另外,如果您看到一个孩子是,您将无法立即返回
null。您还需要计算另一个孩子(如果存在)。
这是我将如何实施:
public int count() // total person count including this object{ int count = 1; // we count this node as 1 if (child1 != null) // if we have a left child, count its size count += child1.count(); if (child2 != null) // if we have a right child, count its size count += child2.count() return count;}您需要考虑两个孩子,即使其中一个是
null。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)