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:
SELECT N, IF(P IS NULL, 'Root', IF((SELECT COUNT(*) FROM BST WHERE P=B.N)>0, 'Inner', 'Leaf'))
FROM BST AS B
ORDER BY N;
SELECT N, IF(P IS NULL, 'Root', IF(B.N IN (SELECT P FROM BST), 'Inner', 'Leaf'))
FROM BST AS B
ORDER BY N;
'Algorithm > SQL' 카테고리의 다른 글
[SQL][HackerRank] Weather Observation Station 17 (0) | 2020.06.10 |
---|---|
[SQL][HackerRank] New Companies (0) | 2020.06.10 |
[SQL][HackerRank] Employee Salaries (0) | 2020.06.09 |
[SQL][HackerRank] Employee Names (0) | 2020.06.09 |
[SQL][HackerRank] Higher Than 75 Marks (0) | 2020.06.09 |