Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e4d50a

Browse files
add 608
1 parentc67932f commit0e4d50a

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ Your ideas/fixes/algorithms are more than welcome!
599599
|613|[Shortest Distance in a Line](https://leetcode.com/problems/shortest-distance-in-a-line/)|[Solution](../master/database/_613.sql)||| Easy|
600600
|612|[Shortest Distance in a Plane](https://leetcode.com/problems/shortest-distance-in-a-plane/)|[Solution](../master/database/_612.sql)||| Medium|
601601
|610|[Triangle Judgement](https://leetcode.com/problems/triangle-judgement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_610.java)||| Easy|
602+
|608|[Tree Node](https://leetcode.com/problems/tree-node/)|[Solution](../master/database/_608.sql) | | | Medium | Union
602603
|607|[Sales Person](https://leetcode.com/problems/sales-person/)|[Solution](../master/database/_607.sql)||| Easy|
603604
|602|[Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/)|[Solution](../master/database/_602.sql)||| Medium|
604605
|597|[Friend Requests I: Overall Acceptance Rate](https://leetcode.com/problems/friend-requests-i-overall-acceptance-rate/)|[Solution](../master/database/_597.sql)||| Easy|

‎database/_608.sql

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--608. Tree Node
2+
--Given a table tree, id is identifier of the tree node and p_id is its parent node's id.
3+
--
4+
--+----+------+
5+
--| id | p_id |
6+
--+----+------+
7+
--| 1 | null |
8+
--| 2 | 1 |
9+
--| 3 | 1 |
10+
--| 4 | 2 |
11+
--| 5 | 2 |
12+
--+----+------+
13+
--Each node in the tree can be one of three types:
14+
--Leaf: if the node is a leaf node.
15+
--Root: if the node is the root of the tree.
16+
--Inner: If the node is neither a leaf node nor a root node.
17+
--Write a query to print the node id and the type of the node. Sort your output by the node id. The result for the above sample is:
18+
--+----+------+
19+
--| id | Type |
20+
--+----+------+
21+
--| 1 | Root |
22+
--| 2 | Inner|
23+
--| 3 | Leaf |
24+
--| 4 | Leaf |
25+
--| 5 | Leaf |
26+
--+----+------+
27+
--Explanation
28+
--
29+
--Node '1' is root node, because its parent node is NULL and it has child node '2' and '3'.
30+
--Node '2' is inner node, because it has parent node '1' and child node '4' and '5'.
31+
--Node '3', '4' and '5' is Leaf node, because they have parent node and they don't have child node.
32+
--
33+
--And here is the image of the sample tree as below:
34+
-- 1
35+
--/ \
36+
-- 2 3
37+
-- / \
38+
-- 4 5
39+
--Note
40+
--
41+
--If there is only one node on the tree, you only need to output its root attributes.
42+
43+
--credit: https://leetcode.com/articles/tree-node/#approach-i-using-union-accepted
44+
select id,'Root'as Typefrom treewhere p_id isnull
45+
union
46+
select id,'Leaf'as Typefrom treewhere id notin
47+
(select distinct p_idfrom tree
48+
where p_idis not null)
49+
and p_idis not null
50+
union
51+
select id,'Inner'as Typefrom treewhere idin
52+
(select distinct p_idfrom tree
53+
where p_idis not null)
54+
and p_idis not null
55+
order by id;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp