HackerRank

[MySQL] Binary Tree Nodes [HackerRank Medium]

치킨먹고싶어요 2022. 5. 26. 13:29

https://www.hackerrank.com/challenges/binary-search-tree-1/problem?isFullScreen=true 

 

Binary Tree Nodes | HackerRank

Write a query to find the node type of BST ordered by the value of the node.

www.hackerrank.com

Question:

You are given a table, BST, containing two columns: N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.

Write a query to find the node type of Binary Tree ordered by the value of the node. 

 

해석:

주어진 BST테이블을 가지고 이진 트리를 나타내라 

 

코드:

SELECT N, CASE 
        WHEN P IS null THEN 'Root'
        WHEN N IN (
            SELECT P
            FROM BST
        ) THEN 'Inner'
        ELSE 'Leaf'
    END AS L
FROM BST
ORDER BY N
cs

CASE WHEN으로 풀 수 있는 간단한 문제 였습니다.

P = NULL이 아니라 P IS NULL 이라는 것 정도만 주의해 주시면 됩니다.