当前位置:网站首页>LeetCode-608. Tree node
LeetCode-608. Tree node
2022-04-23 10:04:00 【Border tramp】
Given a table tree,id Is the number of the tree node , p_id Is its parent node id .
+----+------+
| id | p_id |
+----+------+
| 1 | null |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
+----+------+
Each node in the tree belongs to one of the following three types :
leaf : If this node does not have any child nodes .
root : If this node is the root of the whole tree , That is, there is no parent node .
Internal node : If this node is neither a leaf node nor a root node .
Write a query statement , Output the number and type of all nodes , And sort the results according to the node number . The result of the above example is :
+----+------+
| id | Type |
+----+------+
| 1 | Root |
| 2 | Inner|
| 3 | Leaf |
| 4 | Leaf |
| 5 | Leaf |
+----+------+
explain
node '1' Root node , Because its parent node is NULL , At the same time, it has child nodes '2' and '3' .
node '2' It's an internal node , Because it has a parent node '1' , There are also children '4' and '5' .
node '3', '4' and '5' It's all leaf nodes , Because they all have parent nodes and no child nodes .
The tree in the sample is as follows :
1
/ \
2 3
/ \
4 5
Be careful
If there is only one node in the tree , You just need to output its root attribute .
# Write your MySQL query statement below
SELECT t1.id, 'Leaf' AS Type FROM tree AS t1
WHERE t1.id
NOT IN
(
SELECT DISTINCT p_id
FROM tree
WHERE p_id IS NOT NULL
) AND t1.p_id IS NOT NULL
UNION
SELECT DISTINCT i1.id, 'Inner' AS Type
FROM tree AS i1 INNER JOIN tree AS i2
ON i1.id = i2.p_id
WHERE i1.id = i2.p_id AND i1.p_id IS NOT NULL AND i2.id IS NOT NULL
UNION
SELECT id, 'Root' AS Type
FROM tree WHERE p_id IS NULL;
版权声明
本文为[Border tramp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230955351135.html
边栏推荐
- 中控学习型红外遥控模块支持网络和串口控制
- ansible playbook语法和格式 自动化云计算
- 【无标题】
- 杰理之用户如何最简单的处理事件【篇】
- CSP certification 202203-2 travel plan (multiple solutions)
- 2022年上海市安全员C证考试题库及答案
- Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
- 杰理之通常程序异常情况有哪些?【篇】
- 一文看懂 LSTM(Long Short-Term Memory)
- GCD of p2257 YY (Mobius inversion)
猜你喜欢
Construire neuf capacités de fabrication agile à l'ère métacosmique
Yarn资源调度器
Failureforwardurl and failureurl
中控学习型红外遥控模块支持网络和串口控制
Depth selector
Yarn核心参数配置
2022茶艺师(初级)考试试题模拟考试平台操作
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
2022年上海市安全员C证考试题库及答案
Career planning and implementation in the era of meta universe
随机推荐
Art template template engine
Custom login failure handling
Integral function and Dirichlet convolution
通过流式数据集成实现数据价值(2)
Planning and construction of industrial meta universe platform
SQL调优系列文章之—SQL性能方法论
元宇宙时代的职业规划与执行
(Extended) bsgs and higher order congruence equation
DBA常用SQL语句(2)— SGA和PGA
formatTime时间戳格式转换
Chapter 1 Oracle database in memory related concepts (im-1.1)
Realizing data value through streaming data integration (5) - flow analysis
SQL tuning series - SQL performance methodology
101. Symmetric Tree
Common DBA SQL statements (4) - Top SQL
杰理之用户如何最简单的处理事件【篇】
Pyqt5 and communication
Pyqt5与通信
F-niu Mei's apple tree (diameter combined)
[hdu6868] absolute math (pusher + Mobius inversion)