# 608. Tree Node ###### tags: `SQL` `medium` https://leetcode.com/problems/tree-node/description/ ![](https://i.imgur.com/Hdna0WW.png) ![](https://i.imgur.com/9bwDCbT.png) ![](https://i.imgur.com/Htap0lK.png) 將樹放進SQL的題目 題目要求將樹的type寫上去 root 根 代表為null inner 中間 不為根也不為葉 代表有父及子 也就是p_id裡有的 leaf 葉 只有父沒有子 所以解題方式就是判斷 pid為null肯定根 id不再pid的又不是null 那就是leaf 而剩下的就為inner 這次使用了case when SQL的判斷 而判斷pid也可直接判斷inner 因為pid裡有的就是inner 所以else inner或leaf都行 解題如下 ![](https://i.imgur.com/VOK9dAJ.png)