728x90
※ 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.
■ [Advanced Select] Binary Tree Nodes
https://www.hackerrank.com/challenges/binary-search-tree-1/problem?isFullScreen=true
Q.
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.
A.
SELECT
N,
CASE
WHEN MAX_LEVEL = 1 THEN 'Root'
WHEN MAX_LEVEL = (SELECT MAX(LEVEL) FROM BST CONNECT BY PRIOR N = P) THEN 'Leaf'
ELSE 'Inner'
END AS INFO
FROM
(
SELECT N, P, MAX(LEVEL) MAX_LEVEL
FROM BST
CONNECT BY PRIOR N = P
GROUP BY N, P
)
ORDER BY N;
반응형
'코딩 문제 풀기 ( Algorithm problem solving ) > 해커랭크 ( HackerRank )' 카테고리의 다른 글
[HackerRank][SQL(Oracle)] The Report (0) | 2023.03.29 |
---|---|
[HackerRank][SQL(Oracle)] Contest Leaderboard (0) | 2023.03.29 |
[HackerRank][SQL(Oracle)] Placements (0) | 2023.03.29 |
[HackerRank][SQL(Oracle)] Draw The Triangle 1, 2 (0) | 2023.03.29 |
[HackerRank][SQL(Oracle)] Type of Triangle (0) | 2023.03.29 |