Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- NLP
- 서브쿼리
- 카이제곱분포
- torch
- HackerRank
- MySQL
- update
- LSTM
- SQL 날짜 데이터
- 설명의무
- 논문리뷰
- 표준편차
- inner join
- 자연어 논문
- Window Function
- 짝수
- leetcode
- t분포
- nlp논문
- 그룹바이
- Statistics
- 자연어처리
- GRU
- sql
- airflow
- sigmoid
- CASE
- 자연어 논문 리뷰
- SQL코테
- 코딩테스트
Archives
- Today
- Total
HAZEL
[SQL : HackerRank] Binary Tree Nodes 본문
Binary Tree Nodes
>> 문제
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 distinct BST.N
, CASE
WHEN BST.P IS NULL THEN 'Root'
WHEN BST2.N IS NULL THEN 'Leaf'
ELSE 'Inner'
END
FROM BST
LEFT JOIN BST AS BST2
ON BST.N = BST2.P
ORDER BY BST.N
※ 이 문제에서는 case문을 떠올려서 푸는 것이 중요하다!
join 으로 얻어낸 값 자체도 case로 할 수 있다. 배운건 잊지말고 생각하자!
www.hackerrank.com/challenges/binary-search-tree-1/problem
'DATA ANALYSIS > SQL' 카테고리의 다른 글
[SQL : DELETE / 서브쿼리 / INNER JOIN] LeetCode : 196. Delete Duplicate Emails (0) | 2021.03.09 |
---|---|
[SQL : CASE문 / UPDATE ] LeetCode : 627. Swap Salary (0) | 2021.03.08 |
[SQL : LEFT JOIN ] HackerRank : Placements (0) | 2021.03.02 |
[SQL : HackerRank] Weather Observation Station 3 / Weather Observation Station 19 (0) | 2021.03.01 |
[SQL : HackerRank] Top Competitors (0) | 2021.02.28 |