Binary tree "get open list" SQL query
In this case I think you don't need a recursive query, simply get those records that do not have two child records.


select *
from   mytable
where  id not in(select   parent_id
                 from     mytable
                 group by parent_id
                 having   count(*) &gt; 1);


id | parent_id
-: | --------:
 4 |         2
 5 |         2
 6 |         3
 7 |         4
 8 |         5
 9 |         6
10 |         3


dbfiddle here