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
The Binary Tree below illustrates the sample:
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 NodeType
FROM BST
ORDER BY N;
'SQL > HackerRank' 카테고리의 다른 글
[MySQL/HackerRank] Revising Aggregations - The Count Function (0) | 2024.08.13 |
---|---|
[MySQL/HackerRank] New Companies (0) | 2024.08.12 |
[MySQL/HackerRank] Occupations (0) | 2024.08.11 |
[MySQL/HackerRank] The PADS (0) | 2024.08.11 |
[MySQL/HackerRank] Type of Triangle (0) | 2024.08.11 |