My favorite style when using recursion is to use some kind of wrapper function where the main method calls another that does the grunt job:
private int countLeftNodes(IntTreeNode node){ int totalCount = reallyCountLeftNodes(IntTreeNode node, 0); return totalCount; } private int reallyCountLeftNodes(IntTreeNode n, Int sum){ if (n.left == NULL && n.right == NULL){
Notice how the main function calls another. I think this style is cleaner and more understandable. In addition, the second function has a count variable that you can use.
source share