I am unable to understand why my counter is unable to store the correct value.
This code works when I keep the counter as a universal variable, but it doesn't work if I pass the "count" in the function
public int numberOfLeaves(TreeNode root) {
if(root==null)
return 0;
return leaves(root,0);
}
public int leaves(TreeNode TN,int count){
if(TN.left==null && TN.right==null) {
count++;
}
if(TN.left!=null){
leaves(TN.left,count);
}
if(TN.right!=null){
leaves(TN.right,count);
}
return count;
}