https://www.hackerrank.com/challenges/binary-search-tree-1/problem?isFullScreen=true
Binary Tree Nodes | HackerRank
Write a query to find the node type of BST ordered by the value of the node.
www.hackerrank.com
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:
[My Answer]
SELECT
N,
(case when P is null then 'Root'
when N in (select P from BST) then 'Inner'
else 'Leaf' end) node_type
FROM BST AS B
ORDER BY N
문제 이해만 빨리되면 금방 할수있겠다.
'Code Problems > SQL_ HackerRank' 카테고리의 다른 글
[HackerRank] Revising Aggregations - The Count Function (0) | 2021.11.07 |
---|---|
[HackerRank] New Company (0) | 2021.11.06 |
[HackerRank] Occupations (0) | 2021.10.23 |
[HackerRank] The PADS (0) | 2021.10.04 |
[HackerRank] Type of Triangle (0) | 2021.10.03 |