|
| 1 | +--- |
| 2 | +id: tree-node |
| 3 | +title: Tree Node |
| 4 | +sidebar_label: 0608-Tree-Node |
| 5 | +tags: |
| 6 | +- Database |
| 7 | +description: "SQL Schema: Given a table with nodes, write an SQL query to find all nodes which are the only child nodes." |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem |
| 11 | + |
| 12 | +Given a table `Tree`, write an SQL query to find all nodes which are the only child nodes. |
| 13 | + |
| 14 | +The `Tree` table contains these columns: |
| 15 | + |
| 16 | +- `id`: integer |
| 17 | +- `p_id`: integer (parent id) |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example:** |
| 22 | + |
| 23 | +**Input:** |
| 24 | +Tree table: |
| 25 | +``` |
| 26 | ++----+------+ |
| 27 | +| id | p_id | |
| 28 | ++----+------+ |
| 29 | +| 1 | null | |
| 30 | +| 2 | 1 | |
| 31 | +| 3 | 1 | |
| 32 | +| 4 | 2 | |
| 33 | +| 5 | 2 | |
| 34 | +| 6 | 3 | |
| 35 | +| 7 | 4 | |
| 36 | +| 8 | 5 | |
| 37 | ++----+------+ |
| 38 | +``` |
| 39 | +**Output:** |
| 40 | +``` |
| 41 | ++----+ |
| 42 | +| id | |
| 43 | ++----+ |
| 44 | +| 6 | |
| 45 | +| 7 | |
| 46 | +| 8 | |
| 47 | ++----+ |
| 48 | +``` |
| 49 | +Explanation: Nodes `6`, `7`, and `8` are the only child nodes. |
| 50 | + |
| 51 | +### Constraints |
| 52 | + |
| 53 | +- `id` is the primary key column for this table. |
| 54 | +- Each row of this table contains information about the `id` of a node and the `id` of its parent node. |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Approach |
| 59 | + |
| 60 | +To solve this problem, we need to find nodes that have no siblings. In other words, we need to find nodes where there is only one child per parent. |
| 61 | + |
| 62 | +### Steps: |
| 63 | + |
| 64 | +1. Count the number of children for each parent. |
| 65 | +2. Select the nodes where the parent has exactly one child. |
| 66 | + |
| 67 | +### Solution |
| 68 | + |
| 69 | +#### SQL |
| 70 | + |
| 71 | +```sql |
| 72 | +SELECT |
| 73 | + id |
| 74 | +FROM |
| 75 | + Tree |
| 76 | +WHERE |
| 77 | + p_id IN ( |
| 78 | + SELECT |
| 79 | + p_id |
| 80 | + FROM |
| 81 | + Tree |
| 82 | + GROUP BY |
| 83 | + p_id |
| 84 | + HAVING |
| 85 | + COUNT(*) = 1 |
| 86 | + ); |
| 87 | +``` |
| 88 | +### Explanation |
| 89 | +1) The subquery selects p_id values from the Tree table and groups them by p_id. It then uses the HAVING clause to filter groups with a count of exactly one, meaning those p_id values with only one child. |
| 90 | + |
| 91 | +2) The outer query then selects the id values where the p_id matches the ones returned by the subquery, which ensures that we get nodes that are the only child nodes. |
| 92 | + |
| 93 | +### Complexity Analysis |
| 94 | +**Time Complexity:** O(n log n) |
| 95 | +>Reason: The complexity primarily comes from the grouping and counting operations. Depending on the database system, this can vary, but in general, it is expected to be O(n log n). |
| 96 | +
|
| 97 | +**Space Complexity:** O(n) |
| 98 | +>Reason: The space complexity is mainly due to the storage of intermediate results, which can be linear in the size of the input. |
| 99 | +
|
| 100 | +### References |
| 101 | +**LeetCode Problem:** Tree Node |
0 commit comments