这两个版本之间有什么区别?计算树中的两个版本离开树
public static int countLeaves(IntTreeNode root) {
if (root == null) {
return 0;
} else
return 1 + countLeaves(root.left) + countLeaves(root.right);
}
public static int countLeaves(IntTreeNode root) {
if (root == null) {
return 0;
} else if (root.left == null && root.right == null) {
return 1;
} else
return countLeaves(root.left) + countLeaves(root.right);
}
我在互联网上找不到任何使用第一个版本的东西。 第一个版本是否错误?
我试图在纸上描绘它们,它们看起来是一样的。 但我只想确定一下。
当您运行它时,第一个版本是否会提供正确的结果? – ChiefTwoPencils
@ChiefTwoPencils我测试了几个案例,是的。 – chuck
我不认为他们可以给出相同的结果。 – ChiefTwoPencils