-
leetcode - 608. Tree NodeSQL 문제풀이 2022. 7. 6. 15:57
난이도 : Medium
Table: Tree
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | p_id | int | +-------------+------+ id is the primary key column for this table. Each row of this table contains information about the id of a node and the id of its parent node in a tree. The given structure is always a valid tree.
Each node in the tree can be one of three types:
- "Leaf": if the node is a leaf node.
- "Root": if the node is the root of the tree.
- "Inner": If the node is neither a leaf node nor a root node.
Write an SQL query to report the type of each node in the tree.
Return the result table ordered by id in ascending order.
SELECT id , CASE WHEN p_id IS NULL THEN 'Root' WHEN id IN (SELECT p_id FROM Tree ) THEN 'Inner' ELSE 'Leaf' END type FROM tree
Accepted (5.01%)
'SQL 문제풀이' 카테고리의 다른 글
leecode - 1327. List the Products Ordered in a Period (0) 2022.07.08 (Again) leecode - 612. Shortest Distance in a Plane (0) 2022.07.06 leetcode - 1322. Ads Performance (0) 2022.07.06 leetcode - 1303. Find the Team Size (0) 2022.07.06 leetcode - 1294. Weather Type in Each Country (0) 2022.07.06