본문 바로가기
취준/코팅 테스트

[LeetCode] - 608. Tree Node

by 소복소복뚜벅뚜벅 2024. 4. 10.

문제 

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.

 

" Leef, Root, Inner로 분류하여라" 

 

 

문제 풀이 

 

1)  Root의 경우는  p_id가 Null 인 경우이다. 

 

2) Inner의 경우는 P-id가 id에 속해 있어야 하고, id도 역시 p_id에 속해야 있어야 한다. 

 

3) 그 외의 경우는 leaf 이다. 

 

SELECT id
    ,  CASE WHEN p_id IS NULL THEN 'Root'
            WHEN p_id IN (SELECT id FROM Tree) AND id IN (SELECT p_id FROM Tree) THEN 'Inner'
       ELSE 'Leaf' END AS type  
FROM Tree