Easy to follow, optimal solution w/ explanation.
Intuition\nThe root node is easily detected because it doesn\'t have a parent and thus its p_id is null. The inner nodes are the root of their subtree and thus 
# Intuition\nThe root node is easily detected because it doesn\'t have a parent and thus its `p_id` is `null`. The inner nodes are the root of their subtree and thus their `id` will be found somewhere as a `p_id`. For leaf nodes, their `id` will be never found as `p_id`.\n\n# Approach\nThus, using a left self-join on the condition of `id = p_id` i.e. that a node is also a parent/root in its own subtree, will separate the inner from the leaf nodes; the first ones will have a match and the others won\'t.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```mssql []\nselect distinct t.id\n        , iif(t.p_id is null, \'Root\', iif(tt.id is null, \'Leaf\', \'Inner\')) as type\nfrom tree as t\n    left join tree as tt\n    on t.id = tt.p_id\n\n\n```
['MS SQL Server']