I know that this code should work but it does not. Does anyone know what i am missing? I am trying to get the sum of all nodes in a binary tree.
public int getSum() {
if (this == null) {
return 0;
} else {
return this.value + right.getSum() + left.getSum();
}
}
Your check
this == nullis completely useless. However, you do need to be checking for the existence of your left and right nodes. Try this: