Skip to content

Commit 65e9270

Browse files
committed
task: #608
1 parent c40fbb4 commit 65e9270

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
161161
- [570. Managers with at Least 5 Direct Reports](./leetcode/medium/570.%20Managers%20with%20at%20Least%205%20Direct%20Reports.sql)
162162
- [585. Investments in 2016](./leetcode/medium/585.%20Investments%20in%202016.sql)
163163
- [602. Friend Requests II: Who Has the Most Friends](./leetcode/medium/602.%20Friend%20Requests%20II:%20Who%20Has%20the%20Most%20Friends.sql)
164+
- [608. Tree Node](./leetcode/medium/608.%20Tree%20Node.sql)
164165
- [626. Exchange Seats](./leetcode/medium/626.%20Exchange%20Seats.sql)
165166
- [1045. Customers Who Bought All Products](./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql)
166167
- [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql)

leetcode/medium/608. Tree Node.sql

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Question 608. Tree Node
3+
Link: https://leetcode.com/problems/tree-node/description/?envType=problem-list-v2&envId=database
4+
5+
Table: Tree
6+
7+
+-------------+------+
8+
| Column Name | Type |
9+
+-------------+------+
10+
| id | int |
11+
| p_id | int |
12+
+-------------+------+
13+
id is the column with unique values for this table.
14+
Each row of this table contains information about the id of a node and the id of its parent node in a tree.
15+
The given structure is always a valid tree.
16+
17+
18+
Each node in the tree can be one of three types:
19+
20+
"Leaf": if the node is a leaf node.
21+
"Root": if the node is the root of the tree.
22+
"Inner": If the node is neither a leaf node nor a root node.
23+
Write a solution to report the type of each node in the tree.
24+
25+
Return the result table in any order.
26+
*/
27+
28+
SELECT
29+
t.id,
30+
(CASE
31+
WHEN t.p_id IS NULL THEN 'Root'
32+
WHEN t.id IN (
33+
SELECT DISTINCT t1.p_id
34+
FROM Tree AS t1
35+
) THEN 'Inner'
36+
ELSE 'Leaf'
37+
END) AS type --noqa: RF04
38+
FROM Tree AS t

0 commit comments

Comments
 (0)