Binary Tree Nodes

Sort by

recency

|

2356 Discussions

|

  • + 0 comments

    MS SQL SERVER left join

    select distinct b1.N, CASE WHEN b1.P is null THEN "Root" WHEN b2.N is null THEN "Leaf" WHEN b2.N is not null THEN "Inner" END as NodeType from BST b1 left join BST b2 on b1.N=b2.P order by b1.N

  • + 0 comments

    select N , case when P IS NULL then "Root" when N in (select p from BST where p is not null) then "Inner" else "Leaf" end as types from BST order by n

  • + 0 comments
    SELECT N,
        CASE
            WHEN P IS NULL THEN 'Root'
            WHEN N IN (
                SELECT DISTINCT P 
                FROM BST 
            ) THEN 'Inner'
            ELSE 'Leaf'
        END as P
    FROM BST
    order by N;
    
  • + 0 comments

    MySQL / PostgreSQL

    select N, case when P is null then 'Root' when N not in (select P from BST where P is not null) then 'Leaf' else 'Inner' end as Type from BST order by N;

  • + 0 comments

    select * from (

    SELECT CAST(A.N AS VARCHAR) +' Leaf' as n FROM ( SELECT N FROM BST EXCEPT SELECT P FROM BST ) AS A

    UNION

    SELECT CAST(N AS VARCHAR) +' Root' as n FROM BST WHERE P IS NULL

    UNION

    SELECT CAST(B.N AS VARCHAR) +' Inner' as n FROM (

    SELECT N FROM BST WHERE P IS not NULL INTERSECT SELECT P FROM BST
    ) AS B ) as X order by cast(left(x.n,2) as int);