HackerRank MySQL - Binary Tree Nodes



You are given a table, BST, containing two columns: N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.



  1. SELECT N,
  2. CASE
  3. WHEN P IS NULL THEN 'Root'
  4. WHEN N IN (SELECT P FROM BST) THEN 'Inner'
  5. ELSE 'Leaf'
  6. END
  7. FROM BST
  8. ORDER BY N;

download android app