Binary Tree Nodes
Data Analyst

빅데이터 관련 자료/[SQL] Basic

Binary Tree Nodes

carpe08 2021. 9. 6. 18:55
320x100
320x100

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.

Sample Input

 

 

 

 

 

 

 

 

# 나의 해답

 

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 NODETYPE
FROM BST
ORDER BY N;

320x100
320x100

'빅데이터 관련 자료 > [SQL] Basic' 카테고리의 다른 글

테이블 결합(JOIN)  (0) 2021.11.18
데이터 조회(SELECT)  (0) 2021.11.17
데이터베이스 기초 (MYSQL)  (0) 2021.09.02
Occupations  (0) 2021.08.22
The PADS  (0) 2021.08.14