Easy and Simple Solution Using Union
\nSELECT ID , \'ROOT\' TYPE FROM TREE WHERE P_ID IS NULL\nUNION\nSELECT ID , \'INNER\' TYPE FROM TREE WHERE P_ID IS NOT NULL AND ID IN (SELECT P_ID FROM TREE)\n
```\nSELECT ID , \'ROOT\' TYPE FROM TREE WHERE P_ID IS NULL\nUNION\nSELECT ID , \'INNER\' TYPE FROM TREE WHERE P_ID IS NOT NULL AND ID IN (SELECT P_ID FROM TREE)\nUNION\nSELECT ID , \'LEAF\' TYPE FROM TREE WHERE P_ID IS NOT NULL AND ID NOT IN (SELECT P_ID FROM TREE WHERE P_ID IS NOT NULL)\n\n```
['Union Find', 'MySQL']