Skip to content

task: #608 #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
- [570. Managers with at Least 5 Direct Reports](./leetcode/medium/570.%20Managers%20with%20at%20Least%205%20Direct%20Reports.sql)
- [585. Investments in 2016](./leetcode/medium/585.%20Investments%20in%202016.sql)
- [602. Friend Requests II: Who Has the Most Friends](./leetcode/medium/602.%20Friend%20Requests%20II:%20Who%20Has%20the%20Most%20Friends.sql)
- [608. Tree Node](./leetcode/medium/608.%20Tree%20Node.sql)
- [626. Exchange Seats](./leetcode/medium/626.%20Exchange%20Seats.sql)
- [1045. Customers Who Bought All Products](./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql)
- [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql)
Expand Down
38 changes: 38 additions & 0 deletions leetcode/medium/608. Tree Node.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Question 608. Tree Node
Link: https://leetcode.com/problems/tree-node/description/?envType=problem-list-v2&envId=database

Table: Tree

+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| p_id | int |
+-------------+------+
id is the column with unique values 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 a solution to report the type of each node in the tree.

Return the result table in any order.
*/

SELECT
t.id,
(CASE
WHEN t.p_id IS NULL THEN 'Root'
WHEN t.id IN (
SELECT DISTINCT t1.p_id
FROM Tree AS t1
) THEN 'Inner'
ELSE 'Leaf'
END) AS type --noqa: RF04
FROM Tree AS t
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy