|
| 1 | +/* |
| 2 | +Question 608. Tree Node |
| 3 | +Link: https://leetcode.com/problems/tree-node/description/?envType=problem-list-v2&envId=database |
| 4 | +
|
| 5 | +Table: Tree |
| 6 | +
|
| 7 | ++-------------+------+ |
| 8 | +| Column Name | Type | |
| 9 | ++-------------+------+ |
| 10 | +| id | int | |
| 11 | +| p_id | int | |
| 12 | ++-------------+------+ |
| 13 | +id is the column with unique values for this table. |
| 14 | +Each row of this table contains information about the id of a node and the id of its parent node in a tree. |
| 15 | +The given structure is always a valid tree. |
| 16 | +
|
| 17 | +
|
| 18 | +Each node in the tree can be one of three types: |
| 19 | +
|
| 20 | +"Leaf": if the node is a leaf node. |
| 21 | +"Root": if the node is the root of the tree. |
| 22 | +"Inner": If the node is neither a leaf node nor a root node. |
| 23 | +Write a solution to report the type of each node in the tree. |
| 24 | +
|
| 25 | +Return the result table in any order. |
| 26 | +*/ |
| 27 | + |
| 28 | +SELECT |
| 29 | +t.id, |
| 30 | + (CASE |
| 31 | + WHENt.p_id ISNULL THEN'Root' |
| 32 | + WHENt.idIN ( |
| 33 | +SELECT DISTINCTt1.p_id |
| 34 | +FROM TreeAS t1 |
| 35 | + ) THEN'Inner' |
| 36 | + ELSE'Leaf' |
| 37 | + END)AS type--noqa: RF04 |
| 38 | +FROM TreeAS t |