Skip to content

Commit b61e11f

Browse files
Added Tree node
1 parent fb0c393 commit b61e11f

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

Tree Node/READme.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [Tree Node](https://leetcode.com/problems/tree-node/)
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
[![View Main Folder](https://img.shields.io/badge/View-Main_Folder-971901?)](https://github.com/thecoddiwompler/SQL-Practice-Questions/tree/main)
4+
[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/thecoddiwompler?tab=repositories)
5+
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/thecoddiwompler)
6+
7+
---
8+
9+
## 🛠️ Problem Statement
10+
11+
<b>Table Name : Queue</b>
12+
13+
| Column Name |Type |
14+
| ------------- | ------------- |
15+
| id | int |
16+
| p_id | varchar |
17+
18+
* <b>id is the column with unique values for this table. </b>
19+
* Each row of this table contains information about the id of a node and the id of its parent node in a tree (represented by p_id).
20+
* The given structure is always a valid tree.
21+
<br/>
22+
23+
Each node in the tree can be one of three types:
24+
25+
* "Leaf": if the node is a leaf node.
26+
* "Root": if the node is the root of the tree.
27+
* "Inner": If the node is neither a leaf node nor a root node.
28+
29+
Write a solution to report the type of each node in the tree.
30+
31+
Return the result table in any order.
32+
33+
The result format is in the following example.
34+
35+
![Sample Example](./img/tree1.jpg)
36+
37+
</br>
38+
39+
<details>
40+
<summary>
41+
Input
42+
</summary>
43+
<br>
44+
45+
<b>Table Name: Queue</b>
46+
47+
| id | p_id |
48+
| --- |------ |
49+
| 1 | null |
50+
| 2 | 1 |
51+
| 3 | 1 |
52+
| 4 | 2 |
53+
| 5 | 2 |
54+
55+
</details>
56+
57+
<details>
58+
<summary>
59+
Output
60+
</summary>
61+
<br>
62+
63+
| id | type |
64+
| ---- | ----|
65+
| 1 | Root |
66+
| 2 | Inner |
67+
| 3 | Leaf |
68+
| 4 | Leaf |
69+
| 5 | Leaf |
70+
71+
</details>
72+
73+
---

Tree Node/img/tree1.jpg

10.5 KB
Loading

Tree Node/schema.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
CREATE TABLE IF NOT EXISTS Tree (id int, p_id int);
2+
3+
TRUNCATE TABLE Tree;
4+
5+
INSERT INTO
6+
Tree (id, p_id)
7+
VALUES
8+
('1', NULL);
9+
10+
INSERT INTO
11+
Tree (id, p_id)
12+
VALUES
13+
('2', '1');
14+
15+
INSERT INTO
16+
Tree (id, p_id)
17+
VALUES
18+
('3', '1');
19+
20+
INSERT INTO
21+
Tree (id, p_id)
22+
VALUES
23+
('4', '2');
24+
25+
INSERT INTO
26+
Tree (id, p_id)
27+
VALUES
28+
('5', '2');
29+
30+
COMMIT;

Tree Node/solution.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
WITH cte AS (
2+
SELECT
3+
a.id,
4+
a.p_id,
5+
b.id c_id
6+
FROM
7+
tree a
8+
LEFT JOIN tree b ON a.id = b.p_id
9+
)
10+
SELECT
11+
id,
12+
CASE
13+
WHEN p_id IS NULL THEN 'Root'
14+
WHEN c_id IS NULL THEN 'Leaf'
15+
ELSE 'Inner'
16+
END AS TYPE
17+
FROM
18+
cte
19+
GROUP BY
20+
id,
21+
CASE
22+
WHEN p_id IS NULL THEN 'Root'
23+
WHEN c_id IS NULL THEN 'Leaf'
24+
ELSE 'Inner'
25+
END

0 commit comments

Comments
 (0)