File tree 2 files changed +56
-0
lines changed
2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -599,6 +599,7 @@ Your ideas/fixes/algorithms are more than welcome!
599
599
| 613| [ Shortest Distance in a Line] ( https://leetcode.com/problems/shortest-distance-in-a-line/ ) | [ Solution] ( ../master/database/_613.sql ) | || Easy|
600
600
| 612| [ Shortest Distance in a Plane] ( https://leetcode.com/problems/shortest-distance-in-a-plane/ ) | [ Solution] ( ../master/database/_612.sql ) | || Medium|
601
601
| 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
602
603
| 607| [ Sales Person] ( https://leetcode.com/problems/sales-person/ ) | [ Solution] ( ../master/database/_607.sql ) | | | Easy |
603
604
| 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 |
604
605
| 597| [ Friend Requests I: Overall Acceptance Rate] ( https://leetcode.com/problems/friend-requests-i-overall-acceptance-rate/ ) | [ Solution] ( ../master/database/_597.sql ) | | | Easy |
Original file line number Diff line number Diff line change
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 Type from tree where p_id is null
45
+ union
46
+ select id, ' Leaf' as Type from tree where id not in
47
+ (select distinct p_id from tree
48
+ where p_id is not null )
49
+ and p_id is not null
50
+ union
51
+ select id, ' Inner' as Type from tree where id in
52
+ (select distinct p_id from tree
53
+ where p_id is not null )
54
+ and p_id is not null
55
+ order by id;
You can’t perform that action at this time.
0 commit comments