Studying data
[HackerRank] Advanced Select > Binary Tree Nodes 본문
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
Sample Output
1 Leaf
2 Inner
3 Leaf
5 Root
6 Leaf
8 Inner
9 Leaf
Explanation
The Binary Tree below illustrates the sample:
[MySQL Solution]
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;
문제 링크:
https://www.hackerrank.com/challenges/binary-search-tree-1/problem?isFullScreen=true
'SQL' 카테고리의 다른 글
[HackerRank] Aggregation > Top Earners (0) | 2022.12.29 |
---|---|
[HackerRank] Advanced Select > New Companies (0) | 2022.12.28 |
[HackerRank] Advanced Select > Occupations (0) | 2022.12.28 |
[HackerRank] Basic Select > Weather Observation Station 11 (0) | 2022.12.27 |
[HackerRank] Basic Select > Weather Observation Station 6, 7 (0) | 2022.12.26 |
Comments