当前位置: 首页 > news >正文

力扣-树节点

大家好,我是空空star,本篇带大家了解一道中等的力扣sql练习题。

文章目录

  • 前言
  • 一、题目:608. 树节点
  • 二、解题
    • 1.正确示范①
      • 提交SQL
      • 运行结果
    • 2.正确示范②
      • 提交SQL
      • 运行结果
    • 3.正确示范③
      • 提交SQL
      • 运行结果
    • 4.正确示范④
      • 提交SQL
      • 运行结果
    • 5.其他
  • 总结


前言


一、题目:608. 树节点

给定一个表 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.正确示范①

提交SQL

select id,
case when p_id is null then 'Root' 
when id in (select p_id from tree) then 'Inner'
else 'Leaf' end Type
from tree
order by id;

运行结果

2.正确示范②

提交SQL

select id,
if(p_id is null,'Root',if(id in (select p_id from tree),'Inner','Leaf')) Type
from tree
order by id;

运行结果

3.正确示范③

提交SQL

select id,'Root' Type
from tree
where p_id is null
union all 
select id,'Inner' Type
from tree
where id in(select p_id from tree 
)
and  p_id is not null 
union all 
select id,'Leaf' Type
from tree
where id not in(select p_id from tree where p_id is not null 
)
and p_id is not null
order by id;

运行结果

4.正确示范④

提交SQL

select id,'Root' Type
from tree
where p_id is null
union 
select id,'Inner' Type
from tree
where id in(select p_id from tree 
)
and  p_id is not null 
union 
select id,'Leaf' Type
from tree
where id not in(select p_id from tree where p_id is not null 
)
and p_id is not null
order by id;

运行结果

5.其他


总结

正确示范①思路:
采用case when 语句
case when p_id is null then 'Root'
when id in (select p_id from tree) then 'Inner'
else 'Leaf' end Type
正确示范②思路:
采用 if 语句
if(p_id is null,'Root',if(id in (select p_id from tree),'Inner','Leaf')) Type
正确示范③思路:
采用union all 语句
正确示范④思路:
采用 union 语句

http://www.lryc.cn/news/35700.html

相关文章:

  • MySQL8启动错误“Neither found #innodb_redo subdirectory, nor ib_logfile* files”
  • JVM系列——详细说明Volatile,原子性/可见性,先行发生原则
  • ArcGIS:栅格计算器的运算符和函数详解
  • spring的beanfactory与applicationContext的区别以及继承关系
  • 分享一个 hive on spark 模式下使用 HikariCP 数据库连接池造成的资源泄露问题
  • MySQL 行锁
  • 成为AI架构师的三大能力
  • 博客系统 实现 (前端 + 后端 )代码
  • C语言:如何在cmd命令窗口上玩贪吃蛇游戏
  • Flutter-自定义图标
  • 教学场景应用视频试看预览功能
  • 关于进程与进程调度
  • Redis常用命令及数据类型参数
  • (七十四)大白话深入探索多表关联的SQL语句到底是如何执行的?(2)
  • 销售使用CRM系统集成Excel的五个技巧
  • 过来人告诉你:Java学到什么程度可以找工作?
  • 【Linux】目录结构
  • 37.Java进阶之实现动态编译
  • 【Python百日进阶-Web开发-Vue3】Day549 - Vue3 商城后台 09:Veux4-01基本概念
  • GitLab 解析:为什么市场正在转向一体化 DevSecOps 平台?(附Forrester完整报告下载)
  • ThreadLocal的内部结构和源码探究
  • Linux文件系统(下)
  • 合并链表相关的练习
  • FFmpeg介绍及入门知识
  • ASA材料3D打印服务 抗紫外线材料3D打印服务 抗紫外线模型制作-CASAIM中科院广州电子
  • MySQL workbench数据表和数据结构
  • 网络与信息安全岗位介绍—售后工程师
  • Nowcoder .链表分割
  • 猿创征文 | re:Invent 朝圣之路:“云“行业风向标
  • mysql的distinct和group by的区别