Java:获得后代treenode的第二高祖先

时间:2012-03-06 18:40:32

标签: java treemodel

使用DefaultTreeModeldefaultmutbletreenode的{​​{1}}会返回它的最高祖先,但是你会如何回归这一代以回归它的第二高祖?

getRoot()

所以如何找到root - ancestor 1 - some parent - some child - ancestor 2 - some parent - another parent - some child ,在此分支中给定ancestor 1,每个分支的深度因some child下的每个ancestor节点而异。

我需要从root遍历ancestor 1,并且对于更深的分支,在给定some child的情况下,它会找到some child

1 个答案:

答案 0 :(得分: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.
}