2012-03-06 52 views
0

使用DefaultTreeModeldefaultmutbletreenodegetRoot()返回它的最高祖先,但是如何讓它回到第二個祖先?Java:獲得子孫的第二高祖先treenode

root 
- ancestor 1 
    - some parent 
    - some child 
- ancestor 2 
    - some parent 
    - another parent 
    - some child 

所以如何找到ancestor 1,鑑於some child在這個分支,每個分支的深度不同,爲root下的每個節點ancestor

我的東東從some child移速度達ancestor 1,並進行更深層次的分支,給some child,它會找到ancestor 2

回答

0

試試這個:

TreeNode[] nodeArray = tree.getPathToRoot(nodeInQuestion); 
TreeNode secondFromRoot; 

if ((nodeArray != null) && // I'm not sure this can actually happen. 
    (nodeArray.length > 1)) // current node is not the root node. 
{ 
    secondFromRoot = nodeArray[1]; 
} 
else 
{ 
    ... decide what makes sense here. 
}