Q:

java-loop inside a recursive method

I have a class called TrieNode which has a char key and arrayList of triNode. I am trying to implement a trie.
Structure :

For example the root key is '.' and the children are t,o,s, and p. for the node with key 's', the children are t and p. I want to calculate the number of nodes in the trie. i used this algorithm 
public int size(TrieNode tmp, int n){

    if(tmp.children.isEmpty()){
        return 0;
    }

    for(int i=0;i<tmp.children.size();i++){
        return 1 + size(tmp.children.get(i), n);
    }

    return 1;
}

the problem is i is not unique for each call. so the method is called on the root then s then it child t then t then o until p. when it returns to s again it doesn't call the method on p.
how to fix this?

A:

The problem is that you exit the loop for the first element. If you want to sum all the elements, then you need to sum the recursive results before returning to parent.
public int size(TrieNode tmp, int n){

    int sum = 0;
    for(int i=0;i<tmp.children.size();i++){
        sum +=  size(tmp.children.get(i), n);
    }

    if(/* node has char n */){
      return sum + 1;
    }
    return sum ;
}

