Q:

Stuck finding deepest path in general tree traversal trying to find largest common substring

I am trying to solve the problem of largest common substring between 2 Strings.  I will reduce my problem to the following:  I created a general suffix tree and as per my understanding the largest common substring is the deepest path consisting of nodes that belongs to both strings.  
My test input is: 
String1 = xabc
String2 = abc

It seems that the tree I build is correct but my problem is the following method (I pass the root of the tree initially):  
private void getCommonSubstring(SuffixNode node) {  
   if(node == null)  
    return;  
   if(node.from == ComesFrom.Both){  
    current.add(node);          
   }  
   else{  
    if(max == null || current.size() > max.size()){  
        max = current;              
    }  
    current = new ArrayList<SuffixNode>();   
   }  
   for(SuffixNode n:node.children){  
    getCommonSubstring(n);  
   }  
}       

What I was aiming to do is, in order to find the deepest path with nodes that belong to both strings, I would traverse the tree (pre-order) and add nodes that belong to both strings in a list (current). Once I am in a node that is not part of both I update max list if current is bigger.
But the code is erroneous. And I am confused on how to implement this, since I haven't written code for general (non-binary) trees in ages.
Could you help me figure this out?   
Update:
Modified as per @templatetypedef. Could not make this work either.  
private void getCommonSubstring(SuffixNode node, List<SuffixNode> nodes) {  
   if(node == null)  
    return;  
   if(node.from == ComesFrom.Both){  
    nodes.add(node);              
   }  
   else{  
       if(max == null || current.size() > max.size()){  
       max = nodes;               
    }  
    nodes = new ArrayList<SuffixNode>();  
   }  
   for(SuffixNode n:node.children){  
    List<SuffixNode> tmp = new ArrayList<SuffixNode>(nodes);  
    getCommonSubstring(n, tmp);  
   }  
}  

public class SuffixNode {
    Character character;  
    Collection<SuffixNode> children;  
    ComesFrom from;  
    Character endMarker;  
}  

A:

One issue I see here is that the depth of a node in the suffix tree is not the same as the length of the string along that path.  Each edge in a suffix tree is annotated with a range of characters, so a string encoded by a series of nodes of depth five might have shorter length than a string encoded at depth two.  You will probably need to adjust your algorithm to handle this by tracking the effective length of the string that you've built up so far, rather than the number of nodes in the path that you've traced out up to this point.
A second issue I just noticed is that you seem to only have one current variable that is getting shared across all the different branches of the recursion.  This probably is messing up your state across recursive calls.  For example, suppose that you're at a node and have a path of length three, and that there are two children - the first of which only ends in a suffix of the first string, and the second of which ends in a suffix of both strings.  In that case, if you make the recursive call on the first string, you will end up executing the line
current = new ArrayList<SuffixNode>();  

in the recursive call.  This will then clear all your history, so when you return from this call back up to the original node and descend into the second child node, you will act as if there is no list of nodes built up so far, instead of continuing on from the three nodes you found so far.
To fix this, I'd suggest making current a parameter to the function and then creating a new ArrayList when needed, rather than wiping out the existing ArrayList.  Some of the other logic might have to change as well to account for this.
Given this, I would suggest writing the function in pseudocode like this (since I don't know the particulars of your suffix tree implementations):

If the current node is null, return 0.
If the current node doesn't come from both strings, return 0.
Otherwise:

Set maxLen = 0.
For each child node:

Compute the length of the longest common substring rooted at that node.
Add to that length the number of characters along the edge to that child.
Update maxLen if this exceeds the old value.

Return maxLen.

Hope this helps!

