当前位置:网站首页>LeetCode-608. 树节点
LeetCode-608. 树节点
2022-04-23 09:55:00 【边界流浪者】
给定一个表 tree,id 是树节点的编号, p_id 是它父节点的 id 。
+----+------+
| id | p_id |
+----+------+
| 1 | null |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
+----+------+
树中每个节点属于以下三种类型之一:
叶子:如果这个节点没有任何孩子节点。
根:如果这个节点是整棵树的根,即没有父节点。
内部节点:如果这个节点既不是叶子节点也不是根节点。
写一个查询语句,输出所有节点的编号和节点的类型,并将结果按照节点编号排序。上面样例的结果为:
+----+------+
| id | Type |
+----+------+
| 1 | Root |
| 2 | Inner|
| 3 | Leaf |
| 4 | Leaf |
| 5 | Leaf |
+----+------+
解释
节点 '1' 是根节点,因为它的父节点是 NULL ,同时它有孩子节点 '2' 和 '3' 。
节点 '2' 是内部节点,因为它有父节点 '1' ,也有孩子节点 '4' 和 '5' 。
节点 '3', '4' 和 '5' 都是叶子节点,因为它们都有父节点同时没有孩子节点。
样例中树的形态如下:
1
/ \
2 3
/ \
4 5
注意
如果树中只有一个节点,你只需要输出它的根属性。
# 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;
版权声明
本文为[边界流浪者]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_16542775/article/details/124306091
边栏推荐
- SAP ECC connecting SAP pi system configuration
- 理解作用域
- Odoo 服务器搭建备忘
- [ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
- Formattime timestamp format conversion
- golang力扣leetcode 396.旋转函数
- SAP CR transmission request sequence and dependency check
- Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
- [lnoi2014] LCA - tree chain subdivision - multipoint LCA depth and problems
- [2020wc Day2] F. Clarice picking mushrooms (subtree and query, light and heavy son thought)
猜你喜欢
[untitled]
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
Practice of Flink streaming batch integration in Xiaomi
Windows安装redis并将redis设置成服务开机自启
Cloud identity is too loose, opening the door for attackers
Nvidia最新三维重建技术Instant-ngp初探
ABAP CDs view with association example
Leetcode question bank 78 Subset (recursive C implementation)
ABAP implementation publishes restful services for external invocation example
Dropout技术之随机神经元与随机深度
随机推荐
杰理之通常程序异常情况有哪些?【篇】
SAP 03-amdp CDs table function using 'with' clause
"Gu Yu series" airdrop
第二章 In-Memory 体系结构 (IM-2.2)
Yarn核心参数配置
php 二维数组指定元素相等后相加否则新增
Rain produces hundreds of valleys, and all things grow
Nvidia最新三维重建技术Instant-ngp初探
杰理之通常影响CPU性能测试结果的因素有:【篇】
第一章 Oracle Database In-Memory 相关概念(IM-1.1)
How to obtain geographical location based on photos and how to prevent photos from leaking geographical location
Formattime timestamp format conversion
中控学习型红外遥控模块支持网络和串口控制
Number theory blocking (integer division blocking)
[COCI] lattice (dichotomy + tree divide and conquer + string hash)
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
(Extended) bsgs and higher order congruence equation
1D / 1D dynamic programming learning summary
使用IDEA开发Spark程序
Epidemic prevention registration applet