MSSQL. Tree structure fetching with conditions
WITH cte AS ( SELECT * 
              FROM tree 
              WHERE id = @starting_node
                  UNION ALL
              SELECT tree.* 
              FROM tree, cte
              WHERE tree.parent_id = cte.id
                AND cte.IsCollapsed = 'expanded' -- if collapsed then stop
            )