leetcode 608 樹節點

樹節點

 

select id, 'Root' as Type from tree 
where p_id is null

union

select id, 'Inner' as Type from tree 
where id in 
(
    select distinct p_id from tree where p_id is not null
)
and p_id is not null

union 

select id, 'Leaf' as Type from tree 
where id not in 
(
    select distinct p_id from tree where p_id is not null
)
and p_id is not null

 

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

 

select
    id,
    if(isnull(p_id),
        'Root',
        if(id in (select p_id from tree), 'Inner','Leaf')) as Type
from tree 
order by id

 

==

 
posted @ 2023-04-27 10:31  Carl_ZhangJH  阅读(16)  评论(0)    收藏  举报