Refactor calcSize method

This commit is contained in:
Manuel Thalmann 2022-10-31 21:42:10 +01:00
parent 63da19757e
commit 7a8e7a973d

View file

@ -94,14 +94,18 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
} }
protected int calcSize(TreeNode<T> p) { protected int calcSize(TreeNode<T> p) {
int result = 1; int result = 0;
if (p.left != null) { if (p != null) {
result += calcSize(p.left); result++;
}
if (p.right != null) { if (p.left != null) {
result += calcSize(p.right); result += calcSize(p.left);
}
if (p.right != null) {
result += calcSize(p.right);
}
} }
return result; return result;