I've tried self join but that approch degrading the performance as number of levels are 40.
Please help if we can use any another approach like in Oracle we can implement this by connect by
Input Table
Expected Output