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:
Sample Input
SELECT N,
CASE
WHEN P IS NULL THEN 'Root'
WHEN N NOT IN (SELECT P FROM BST WHERE P IS NOT NULL) THEN 'Leaf'
ELSE 'Inner'
END AS NODE_TYPE
FROM BST
ORDER BY N;
- N 컬럼은 노드값, P는 부모 값을 의미
- root: P가 null인 경우
leaf: 잎은 가지 가장 끝에 있는 것으로 하위 노드를 갖지 않는 경우 (즉, 해당 N 값이 다른 곳에서 P로 참조되지 않는 경우)
inner: 그 외 나머지 경우
- 각 노드 종류에 따라 조건을 설정하기 위해 CASE WHEN 문을 사용한다.
[오늘의 코드 131] [HackerRank] Weather Observation Station 11 (0) | 2024.10.30 |
---|---|
[오늘의 코드 130] [HackerRank] Weather Observation Station 1 (0) | 2024.10.27 |
[오늘의 코드 128] [HackerRank] The PADS (0) | 2024.10.23 |
[오늘의 코드 127] [HackerRank] Japanese Cities' Names (0) | 2024.10.21 |
[오늘의 코드 126] [HackerRank] African Cities (0) | 2024.10.19 |