[23.09.12] Oracle SQL _Binary Tree Nodes

YS CHOI·2023년 9월 12일
1

SQL

목록 보기
2/4
post-thumbnail

테이블 구조

문제1.

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

SELECT n
       ,CASE WHEN p IS NULL THEN 'Root'
             WHEN n NOT IN (SELECT DISTINCT p FROM bst WHERE p is NOT NULL) THEN 'Leaf'
             ELSE 'Inner' END AS tree
FROM bst
ORDER BY n
;

Null 이 포함되면 숫자형으로 반환되지 않으므로,
case when 두번째 문에서 'IS NOT NULL' 추가.

0개의 댓글