Skip to content

Latest commit

 

History

History
21 lines (18 loc) · 436 Bytes

File metadata and controls

21 lines (18 loc) · 436 Bytes

608. Tree Node

Use a case statement to check for the different types of nodes.

If the p_id is null, then it is a root.
If the id is in the list of p_id's (meaning they are another node's parent), then it is a inner node.
Otherwise, it is a leaf node.

SELECT id,
CASE
  WHEN ISNULL(p_id) THEN 'Root'
  WHEN id IN (
    SELECT p_id
    FROM Tree
  ) THEN 'Inner'
  ELSE 'Leaf'
END AS type
FROM Tree
ORDER BY id;