SQL: Tree Node
Medium
SQL — Subqueries
SQL
CASE
Subquery
Classify tree nodes as Root, Inner, or Leaf.
Constraints
Write standard SQL (MySQL 8.0 compatible).
Examples
Example 1:
Input: id parent
1 null
2 1
3 1
4 2
Output:
Example 2:
Input: id parent
1 null
2 1
3 1
4 2
Output:
Hints
CASE with subquery on parent/child.
Tests:
Runtime:
Memory:
Test
Input:
Expected:
Got:
Click Run Code to test against sample cases, or Submit to test against all cases.
▲ Console