[코드카타] SQL 72 Binary Tree Nodes

Data_Student·2024년 12월 20일
0

코드카타

목록 보기
82/82

[코드카타] SQL 72 Binary Tree Nodes

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

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. 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'
                when n in (select p from bst) then 'Inner'
                else 'Leaf' end as type
from bst
order by n
Root와 Inner, Leaf를 어떻게 나눌 수 있는지 고민
Root - P값이 없으면 시작이기 때문에 Root
Inner - N값이 P값에 포함되어 있으면 Inner
Leaf - N값과 P값이 있지만 N값이 P값에 포함되어 있지 않으면 Leaf

0개의 댓글