帮助树递归

帮助树递归,第1张

帮助树递归

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



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5506665.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-13
下一篇2022-12-13

发表评论

登录后才能评论

评论列表(0条)

    保存