File tree Expand file tree Collapse file tree 2 files changed +106
-1
lines changed Expand file tree Collapse file tree 2 files changed +106
-1
lines changed Original file line number Diff line number Diff line change 11<!-- markdownlint-disable MD041 -->
22[ ![ eevveerryyddaayy release] ( https://img.shields.io/badge/eevveerryyddaayy-v1.3.0-blue.svg )] ( https://github.com/ggeerraarrdd/eevveerryyddaayy/ )
3- [ ![ Solved] ( https://img.shields.io/badge/solved-198 -green.svg )] ( #index )
3+ [ ![ Solved] ( https://img.shields.io/badge/solved-199 -green.svg )] ( #index )
44<!-- markdownlint-enable MD041 -->
55
66# SQL Everyday
@@ -232,6 +232,7 @@ Because this project necessitated a framework to enable consistent daily practic
232232| 196 | [ Winning Candidate] ( https://leetcode.com/problems/winning-candidate/description/ ) | [ Solution] ( solutions/196_winning_candidate.md ) | LeetCode | Medium | |
233233| 197 | [ Get Highest Answer Rate Question] ( https://leetcode.com/problems/get-highest-answer-rate-question/ ) | [ Solution] ( solutions/197_get_highest_answer_rate_question.md ) | LeetCode | Medium | |
234234| 198 | [ Count Student Number in Departments] ( https://leetcode.com/problems/count-student-number-in-departments/description/ ) | [ Solution] ( solutions/198_count_student_number_in_departments.md ) | LeetCode | Medium | |
235+ | 199 | [ Tree Node] ( https://leetcode.com/problems/tree-node/description/ ) | [ Solution] ( solutions/199_tree_node.md ) | LeetCode | Medium | |
235236<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
236237<!-- - cSpell:enable --->
237238
Original file line number Diff line number Diff line change 1+ # SQL Everyday \# 199
2+
3+ ## Tree Node
4+
5+ Site: LeetCode\
6+ Difficulty per Site: Medium
7+
8+ ## Problem
9+
10+ Write a solution to report the type of each node in the tree.
11+
12+ Return the result table in ** any order** . [[ Full Description] ( https://leetcode.com/problems/tree-node/description/ )]
13+
14+ ## Submitted Solution
15+
16+ ``` sql
17+ -- Submitted Solution
18+ WITH cte AS (
19+ SELECT
20+ t1 .id AS node
21+ ,COUNT (DISTINCT t1 .p_id ) AS parent_nodes
22+ ,COUNT (DISTINCT t2 .p_id ) AS child_nodes
23+ FROM Tree AS t1
24+ LEFT JOIN Tree as t2 ON t1 .id = t2 .p_id
25+ GROUP BY node
26+ )
27+ SELECT
28+ node AS id
29+ ,CASE WHEN parent_nodes = 0 THEN ' Root'
30+ WHEN parent_nodes > 0 AND child_nodes = 0 THEN ' Leaf'
31+ ELSE ' Inner' END AS type
32+ FROM cte
33+ ;
34+ ```
35+
36+ ## Site Solution
37+
38+ ``` sql
39+ -- LeetCode Solution
40+ -- Approach 1: Using `UNION`
41+ SELECT
42+ id, ' Root' AS Type
43+ FROM
44+ tree
45+ WHERE
46+ p_id IS NULL
47+
48+ UNION
49+
50+ SELECT
51+ id, ' Leaf' AS Type
52+ FROM
53+ tree
54+ WHERE
55+ id NOT IN (SELECT DISTINCT
56+ p_id
57+ FROM
58+ tree
59+ WHERE
60+ p_id IS NOT NULL )
61+ AND p_id IS NOT NULL
62+
63+ UNION
64+
65+ SELECT
66+ id, ' Inner' AS Type
67+ FROM
68+ tree
69+ WHERE
70+ id IN (SELECT DISTINCT
71+ p_id
72+ FROM
73+ tree
74+ WHERE
75+ p_id IS NOT NULL )
76+ AND p_id IS NOT NULL
77+ ORDER BY id;
78+
79+ -- Approach 2: Using flow control statement CASE
80+ SELECT
81+ id AS ` Id` ,
82+ CASE
83+ WHEN tree .id = (SELECT atree .id FROM tree atree WHERE atree .p_id IS NULL )
84+ THEN ' Root'
85+ WHEN tree .id IN (SELECT atree .p_id FROM tree atree)
86+ THEN ' Inner'
87+ ELSE ' Leaf'
88+ END AS Type
89+ FROM
90+ tree
91+ ORDER BY ` Id`
92+ ;
93+ ```
94+
95+ ## Notes
96+
97+ TBD
98+
99+ ## NB
100+
101+ TBD
102+
103+ Go to [ Index] ( ../?tab=readme-ov-file#index ) \
104+ Go to [ Overview] ( ../?tab=readme-ov-file )
You can’t perform that action at this time.
0 commit comments