[Hackerrank] SQL - Binary Tree Nodes

DMIS·2022년 3월 29일
0

SQL

목록 보기
37/48
post-thumbnail

문제

풀이

if 문을 사용하여 다음과 같이 작성할 수 있다.

select 
    n,
    if(p is null, 'Root', 
       if(n in (select distinct p from bst), 'Inner', 'Leaf'))
    as nodetype
from bst
order by n

case 문을 사용하여 작성할 수도 있다. 그런데 이상하게 if 문이 먼저 생각난다.

select 
    n,
    (case when p is null then 'Root'
    when n in (select distinct p from bst) then 'Inner'
    else 'Leaf' end) as nodetype
from bst
order by n
profile
Data + Math

0개의 댓글