티스토리 뷰

728x90

Binary Tree Nodes

You are given a table, BST, containing two columns: 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 
    CASE WHEN P IS NULL THEN CONCAT(N, ' Root')
        WHEN P IS NOT NULL AND N IN (SELECT DISTINCT P FROM BST) THEN CONCAT(N, ' Inner')
        ELSE CONCAT(N, ' Leaf')
    END 
FROM BST
ORDER BY N;


11/15일 2차풀이 - 3분

728x90
LIST

'Programming > MySQL' 카테고리의 다른 글

[MySQL/Hackerrank] Revising Aggregations  (0) 2021.11.03
[MySQL/Hackerrank] New Companies  (0) 2021.11.02
[MySQL/Hackerrank] Occupations  (0) 2021.11.02
[MySQL/Hackerrank] The PADS  (0) 2021.11.02
[MySQL/Hackerrank] Type of Triangle  (0) 2021.11.02
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함