본문 바로가기

Database/SQL

Hackerrank / Binary Tree Nodes

Sample Input

Sample Output

1 Leaf
2 Inner
3 Leaf
5 Root
6 Leaf
8 Inner
9 Leaf


Explanation

The Binary Tree below illustrates the sample:

Solution

SELECT N,
    (CASE WHEN N NOT IN (SELECT distinct P FROM BST where P is not null) THEN 'Leaf'
        WHEN P is null then 'Root'
        ELSE 'Inner' 
        END) AS NODE
FROM BST
order by N;

Case when을 활용하여 조건문 작성.

'Database > SQL' 카테고리의 다른 글

Hackerrank / Ollivander's Inventory  (0) 2022.01.22
Hackerrank / The Report  (0) 2022.01.22
Hackerrank / New Companies  (0) 2021.12.03
Hackerrank / OCCUPATIONS  (0) 2021.11.30
Hackerrank / The PADS  (0) 2021.11.30