Binary Tree Nodes

Sort by

recency

|

2461 Discussions

|

  • + 0 comments
    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 node_type
    from bst
    order by 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 Type
    FROM BST
    ORDER BY N;
    
  • + 0 comments

    SELECT N, CASE WHEN P IS NULL THEN 'Root' WHEN N IN (SELECT P FROM BST) THEN 'Inner' ELSE 'Leaf' END AS Type FROM BST ORDER BY N;

  • + 0 comments
    /*
    Enter your query here.
    Condition 1: Root node is the node does not have a parent node P = NULL
    Condition 2: Inner nodes have a parent node (P IS NOT NULL) AND are parent to other nodes (N in P)
    Condition 3: Else Leaf
    */
    
    SELECT N, 
    CASE 
        WHEN P IS NULL THEN 'Root'
        WHEN P IS NOT NULL AND N IN (SELECT DISTINCT P FROM BST) THEN 'Inner'
        ELSE 'Leaf'
    END AS type
    FROM BST
    ORDER BY N;
    
  • + 0 comments

    select n, case when p is null then 'Root' when n = any (select n where n in (select p from BST ) )then 'Inner' else 'Leaf' end as abc from BST order by n