1、题目描述
来源:力扣(LeetCode)
2、解题思路
其实就是分类,用case when then end
1# 对所有父节点p_id
,进行子节点查询,select p_id,count(id) as s_id from tree group by p_id
2# 2表连接,一个作为父节点查询,一个作为子节点查询,条件是t1.id=cou.p_id
3# 按数量分类
3、提交记录
select id,case when p_id is null then 'Root'
when s_id is null then 'Leaf'
else 'Inner' end as Type
from(
select t1.id,t1.p_id,s_id
from tree t1 left join (select p_id,count(id) as s_id from tree group by p_id) cou
on t1.id=cou.p_id) w```