LeetCode - 608. Tree Node (MySQL)

조민수·2024년 6월 22일
0

LeetCode

목록 보기
42/64
post-custom-banner

Medium, SQL - CASE


문제

Each node in the tree can be one of three types:

  • "Leaf": if the node is a leaf node.
  • "Root": if the node is the root of the tree.
  • "Inner": If the node is neither a leaf node nor a root node.

Write a solution to report the type of each node in the tree.

Return the result table in any order.

The result format is in the following example.


풀이

  • 트리 구조의 데이터를 확인하는 문제
  • 그렇게 어렵진 않았다.
SELECT id,
CASE
    WHEN p_id IS NULL THEN 'Root'
    WHEN id IN (SELECT DISTINCT(p_id) FROM Tree) THEN 'Inner'
    ELSE 'Leaf'
END AS type
FROM Tree 
profile
사람을 좋아하는 Front-End 개발자
post-custom-banner

0개의 댓글