Binary Tree Nodes

  • + 0 comments

    This solution may be more performant for very large tables as it doesn't require the creation of a whole new DISTINCT set

    SELECT
        N,
        CASE
            WHEN P IS NULL THEN 'Root'
            WHEN EXISTS (SELECT 1 FROM BST AS B2 WHERE B2.P = B1.N) THEN 'Inner'
            ELSE 'Leaf'
        END
    FROM BST AS B1
    ORDER BY N