I want to count how many leaf node in a trie structure by counting how many words there are in the trie, but my code is not updating the counting value, instead it always reset back to 0.
int num = 0;
public int countLeafNodes() {
for (char c : children.keySet()) {
Trie node = children.get(c);
System.out.println(c);
if (node.isWord) {
num++;
System.out.println(num);
}
node.countLeafNodes();
}
return num;
}