[Hacker Rank_MySQL]문제 풀이#Binary Tree Nodes 본문

IT/코테문제

[Hacker Rank_MySQL]문제 풀이#Binary Tree Nodes

호랑구야 2022. 3. 1. 09:03

SQL > Advanced Select > Binary Tree Nodes

 

Write a query to find the node type of Binary Tree ordered by the value of the node. Output one of the following for each node:

  • Root: If node is root node.
  • Leaf: If node is leaf node.
  • Inner: If node is neither root nor leaf node.

 

빨간 글씨는 내가 문제를 풀 때 중요하다고 생각한 부분이다.

SELECT N,
CASE
    WHEN P IS NULL
    THEN 'Root'
    ELSE CASE
        WHEN N IN (SELECT P FROM BST)
        THEN 'Inner'
        ELSE 'Leaf'
        END
END
FROM BST
ORDER BY N ASC
반응형
Comments