Skip to content

Commit 95372b1

Browse files
committed
[LeetCode Sync] Runtime - 495 ms (33.04%), Memory - 0.0B (100.00%)
1 parent 820fc82 commit 95372b1

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Solutions/0608-tree-node/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<p>Table: <code>Tree</code></p>
2+
3+
<pre>
4+
+-------------+------+
5+
| Column Name | Type |
6+
+-------------+------+
7+
| id | int |
8+
| p_id | int |
9+
+-------------+------+
10+
id is the column with unique values for this table.
11+
Each row of this table contains information about the id of a node and the id of its parent node in a tree.
12+
The given structure is always a valid tree.
13+
</pre>
14+
15+
<p>&nbsp;</p>
16+
17+
<p>Each node in the tree can be one of three types:</p>
18+
19+
<ul>
20+
<li><strong>&quot;Leaf&quot;</strong>: if the node is a leaf node.</li>
21+
<li><strong>&quot;Root&quot;</strong>: if the node is the root of the tree.</li>
22+
<li><strong>&quot;Inner&quot;</strong>: If the node is neither a leaf node nor a root node.</li>
23+
</ul>
24+
25+
<p>Write a solution to report the type of each node in the tree.</p>
26+
27+
<p>Return the result table in <strong>any order</strong>.</p>
28+
29+
<p>The result format is in the following example.</p>
30+
31+
<p>&nbsp;</p>
32+
<p><strong class="example">Example 1:</strong></p>
33+
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/22/tree1.jpg" style="width: 304px; height: 224px;" />
34+
<pre>
35+
<strong>Input:</strong>
36+
Tree table:
37+
+----+------+
38+
| id | p_id |
39+
+----+------+
40+
| 1 | null |
41+
| 2 | 1 |
42+
| 3 | 1 |
43+
| 4 | 2 |
44+
| 5 | 2 |
45+
+----+------+
46+
<strong>Output:</strong>
47+
+----+-------+
48+
| id | type |
49+
+----+-------+
50+
| 1 | Root |
51+
| 2 | Inner |
52+
| 3 | Leaf |
53+
| 4 | Leaf |
54+
| 5 | Leaf |
55+
+----+-------+
56+
<strong>Explanation:</strong>
57+
Node 1 is the root node because its parent node is null and it has child nodes 2 and 3.
58+
Node 2 is an inner node because it has parent node 1 and child node 4 and 5.
59+
Nodes 3, 4, and 5 are leaf nodes because they have parent nodes and they do not have child nodes.
60+
</pre>
61+
62+
<p><strong class="example">Example 2:</strong></p>
63+
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/22/tree2.jpg" style="width: 64px; height: 65px;" />
64+
<pre>
65+
<strong>Input:</strong>
66+
Tree table:
67+
+----+------+
68+
| id | p_id |
69+
+----+------+
70+
| 1 | null |
71+
+----+------+
72+
<strong>Output:</strong>
73+
+----+-------+
74+
| id | type |
75+
+----+-------+
76+
| 1 | Root |
77+
+----+-------+
78+
<strong>Explanation:</strong> If there is only one node on the tree, you only need to output its root attributes.
79+
</pre>
80+
81+
<p>&nbsp;</p>
82+
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/binary-tree-nodes/description/" target="_blank"> 3054: Binary Tree Nodes.</a></p>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT
2+
id,
3+
CASE
4+
WHEN p_id IS NULL THEN "Root"
5+
WHEN id IN (SELECT p_id FROM Tree) THEN "Inner"
6+
ELSE "Leaf"
7+
END AS TYPE
8+
FROM Tree

0 commit comments

Comments
 (0)