AC Solution

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

  • 1

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

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

  • 0

    @richarddia Your solution is very helpful. Thanks!

  • 1

    Simlar one with case

      select Id,
          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.