AC Solution


  • 6
    R
    select T.id, 
    IF(isnull(T.p_id), 'Root', IF(T.id in (select p_id from tree), 'Inner', 'Leaf')) Type 
    from tree T
    

  • 1
    M

    Mine is similar.. but not quite as succinct...

    select t1.id as 'Id', (if (t1.p_id is NULL, 'Root', if ((select distinct p_id from tree where t1.id = p_id) is not NULL, 'Inner', 'Leaf'))) as 'Type' from tree as t1 order by t1.id;


  • 0

    @richarddia Your solution is very helpful. Thanks!


  • 1

    Simlar one with case

      select Id,
      case 
          when p_id is null then "Root"
          when (p_id is not null and id in (select p_id from Tree)) then "Inner"
          else "Leaf"
      end as Type
      from tree

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.