public static boolean isAncestor ( int idx1 , int idx2 , int [ ] parents ) { int anc = parents [ idx2 ] ; while ( anc != - 1 ) { if ( anc == idx1 ) { return true ; } anc = parents [ anc ] ; } return false ; }
Checks whether idx1 is the ancestor of idx2 . If idx1 is the parent of idx2 this will return true but if idx1 == idx2 it will return false .