코딩테스트/리트코드(LeetCode) - SQL Study Plan

리트코드(LeetCode) SQL 문제풀이 - 608. Tree Node

진한색 2023. 1. 23. 20:09

MySQL 문제 608. Tree Node

 

select id,'Root' as type
    from Tree
    where p_id IS NULL

union

select id,'Leaf' as type
    from Tree
    where id NOT IN (select distinct p_id from Tree
        where p_id IS NOT NULL)
    AND p_id IS NOT NULL

union

select id,'Inner' as type
    from Tree
    where id IN (select distinct p_id from Tree
        where p_id IS NOT NULL)
    AND p_id IS NOT NULL
728x90