LeetCode # 608. 树节点

608. 树节点

题目

表:Tree

±------------±-----+

| Column Name | Type |

±------------±-----+

| id | int |

| p_id | int |

±------------±-----+

id 是该表中具有唯一值的列。

该表的每行包含树中节点的 id 及其父节点的 id 信息。

给定的结构总是一个有效的树。

树中的每个节点可以是以下三种类型之一:

  • "Leaf":节点是叶子节点。
  • "Root":节点是树的根节点。
  • "lnner":节点既不是叶子节点也不是根节点。
    编写一个解决方案来报告树中每个节点的类型。

以 任意顺序 返回结果表。

结果格式如下所示。

示例 1:

输入:

Tree table:

±---±-----+

| 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 是根节点,因为它的父节点为空,并且它有子节点 2 和 3。

节点 2 是一个内部节点,因为它有父节点 1 和子节点 4 和 5。

节点 3、4 和 5 是叶子节点,因为它们有父节点而没有子节点。

分析

单独查询,结果合并,针对节点的不同条件去查找,然后合并结果

题解

sql 复制代码
# 分步查询
# 根节点的父节点为空
-- select id, 'Root' type from tree where p_id is NULL

-- # 叶子节点没有子节点,但有父节点
-- select id, 'Leaf' type from tree where id not in (
--     # 没有子节点就是在p_id列里不存在
--     select distinct p_id from tree where p_id is not NULL
-- ) and p_id is not NULL # 并且自己有父节点

-- # 内部节点,有孩子有父亲节点
-- select id, 'Inner' type from tree where id in(
--     # 自己也要出现在p_id列作为别人的父节点
--     select distinct p_id from tree where p_id is not NULL
-- ) and p_id is not NULL

# 将分步步骤连起来
# 根节点的父节点为空
select id, 'Root' type from tree where p_id is NULL
union
# 叶子节点没有子节点,但有父节点
select id, 'Leaf' type from tree where id not in (
    # 没有子节点就是在p_id列里不存在
    select distinct p_id from tree where p_id is not NULL
) and p_id is not NULL # 并且自己有父节点
union
# 内部节点,有孩子有父亲节点
select id, 'Inner' type from tree where id in(
    # 自己也要出现在p_id列作为别人的父节点
    select distinct p_id from tree where p_id is not NULL
) and p_id is not NULL
order by id
相关推荐
这个DBA有点耶6 小时前
从症状到根因:数据库监控指标的分层方法与静态/动态阈值策略
数据库·mysql·代码规范
SelectDB6 小时前
时间旅行与快照能力速查:Doris 侧三条落地路径
大数据·数据库·数据分析
SelectDB6 小时前
云数仓成本结构拆解速查:五层计费 vs Doris / 托管形态
大数据·数据库·数据分析
SelectDB6 小时前
什么场景能承接与建议保留:Doris vs Snowflake 边界速查
大数据·数据库·数据分析
这个DBA有点耶8 小时前
DELETE没删多少数据,表却越来越大——InnoDB删除空洞与页合并失效的根因
数据库·mysql·代码规范
ly768911 小时前
PostgreSQL 备份恢复策略:pg_basebackup、WAL 归档与 PITR 在企业 Java 系统中的落地
java·数据库·postgresql·pitr·pg_basebackup·wal 归档
小羊没烦恼!12 小时前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp
注塑机数据采集联网12 小时前
如何解决注塑机数据采集联网对接MES系统数据WiFi传输数据不稳问题
大数据·数据库·物联网·数据挖掘·数据分析
黄俊懿12 小时前
【架构师从入门到进阶】第六章:服务集群优化——第八节:线程数公式
java·服务器·数据库·分布式·性能优化·架构·系统架构