I am trying to find the solution for a problem where i have something like A > B B > C B > D C > D And I should get the answer as A > B > C > D.