-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3054-BinaryTreeNodes.sql
More file actions
122 lines (116 loc) · 2.8 KB
/
3054-BinaryTreeNodes.sql
File metadata and controls
122 lines (116 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
-- 3054. Binary Tree Nodes
-- Table: Tree
-- +-------------+------+
-- | Column Name | Type |
-- +-------------+------+
-- | N | int |
-- | P | int |
-- +-------------+------+
-- N is the column of unique values for this table.
-- Each row includes N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.
-- Write a solution to find the node type of the Binary Tree. Output one of the following for each node:
-- Root: if the node is the root node.
-- Leaf: if the node is the leaf node.
-- Inner: if the node is neither root nor leaf node.
-- Return the result table ordered by node value in ascending order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Tree table:
-- +---+------+
-- | N | P |
-- +---+------+
-- | 1 | 2 |
-- | 3 | 2 |
-- | 6 | 8 |
-- | 9 | 8 |
-- | 2 | 5 |
-- | 8 | 5 |
-- | 5 | null |
-- +---+------+
-- Output:
-- +---+-------+
-- | N | Type |
-- +---+-------+
-- | 1 | Leaf |
-- | 2 | Inner |
-- | 3 | Leaf |
-- | 5 | Root |
-- | 6 | Leaf |
-- | 8 | Inner |
-- | 9 | Leaf |
-- +---+-------+
-- Explanation:
-- - Node 5 is the root node since it has no parent node.
-- - Nodes 1, 3, 6, and 8 are leaf nodes because they don't have any child nodes.
-- - Nodes 2, 4, and 7 are inner nodes as they serve as parents to some of the nodes in the structure.
-- Create table If Not Exists Tree (N int,P int)
-- Truncate table Tree
-- insert into Tree (N, P) values ('1', '2')
-- insert into Tree (N, P) values ('3', '2')
-- insert into Tree (N, P) values ('6', '8')
-- insert into Tree (N, P) values ('9', '8')
-- insert into Tree (N, P) values ('2', '5')
-- insert into Tree (N, P) values ('8', '5')
-- insert into Tree (N, P) values ('5', 'None')
-- Write your MySQL query statement below
SELECT
t.N,
CASE
WHEN COUNT(p.N) = 0 THEN "Root" -- 没有父类为 root
WHEN COUNT(c.N) = 0 THEN "Leaf" -- 没有儿子的为 leaf
ELSE "Inner" -- 有父有子的
END AS Type
FROM
Tree AS t
LEFT JOIN
Tree AS p -- parent
ON
t.P = p.N
LEFT JOIN
Tree AS c -- child
ON
t.N = c.P
GROUP BY
t.N
ORDER BY
t.N
SELECT
t.N,
CASE
WHEN t.P IS NULL THEN "Root" -- 没有父类为 root
WHEN COUNT(c.N) = 0 THEN "Leaf" -- 没有儿子的为 leaf
ELSE "Inner" -- 有父有子的
END AS Type
FROM
Tree AS t
LEFT JOIN
Tree AS c -- child
ON
t.N = c.P
GROUP BY
t.N
ORDER BY
t.N
WITH t AS (
SELECT
a.*,
b.N as S
FROM
tree AS a
LEFT JOIN
tree AS b
ON
b.P = a.N
)
SELECT
DISTINCT N,
CASE
WHEN P IS Null THEN 'Root'
WHEN S IS Null THEN 'Leaf'
ELSE 'Inner'
END AS Type
FROM
t
ORDER BY
t.N