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
Question:
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.
해석:
주어진 BST테이블을 가지고 이진 트리를 나타내라
코드:
SELECT N, CASE
WHEN P IS null THEN 'Root'
WHEN N IN (
SELECT P
FROM BST
) THEN 'Inner'
ELSE 'Leaf'
END AS L
FROM BST
ORDER BY N
|
cs |
CASE WHEN으로 풀 수 있는 간단한 문제 였습니다.
P = NULL이 아니라 P IS NULL 이라는 것 정도만 주의해 주시면 됩니다.
'HackerRank' 카테고리의 다른 글
[HackerRank Easy] Weather Observation Station 5[MySQL] (0) | 2022.05.27 |
---|---|
[HackerRank Medium] Symmetric Pairs [MySQL] (0) | 2022.05.26 |
[MySQL] The Report [HackerRank Medium] (0) | 2022.05.26 |
[해커랭크 HackerRank] Population Census [MySQL] (0) | 2022.05.26 |
[MySQL] African Cities[HackerRank Easy] (0) | 2022.05.26 |