In the code snippet below I am calling nodeDepths(root, depth, depthSum)
and expecting updated value in depthSum
but every time it's returning 0 to me
public static int nodeDepths(BinaryTree root) {
Integer depthSum = 0;
Integer depth = 0;
nodeDepths(root, depth, depthSum);
return depthSum;
}
public static void nodeDepths(BinaryTree root, int depth, Integer depthSum) {
depth = depth + 1;
depthSum = depthSum + depth;
if (root.left == null && root.right == null) {
return;
}
nodeDepths(root.left, depth, depthSum);
nodeDepths(root.right, depth, depthSum);
}
Note: I have not mentioned the problem scenario and logical part as that is not an issue. The issue I am facing is the "depthSum reference variable not getting updated in caller method"