- Your comments arn't really helpful, and so I'd remove them. This is as the function names say all that you would want to know.
- Using the inverse of 'split the line, change the sign''split the line, change the sign', more commonly know as expanding notDeMorgan's laws, you can change
isInternalto be much simpler. Rather than using \$\overline{A} \lor \overline{B}\$ your can use \$\overline{A \land B}\$. - Since
isInternalis the inverse ofisLeafyou can just check thatisLeaf(n.left)is not the same asisLeaf(n.right), as if it's not then there is one leaf and one internal.
And so you can change isWantedNode and isLeaf to:
private static boolean isWantedNode(TreeNode n) { if (n.left == null || n.right == null) return false; return isLeaf(n.left) != isLeaf(n.right); } private static boolean isLeaf(TreeNode n) { return n.left == null && n.right == null; } You may also want to change your return statment in strangeCount from a turnery, to addition of result and a turnery. And so I'd use:
private static int strangeCount(TreeNode h) { if (h == null) { return 0; } return strangeCount(h.left) + strangeCount(h.right) + (isWantedNode(h)?1:0); }