From 335b30f6692f888ae6a1fa2e6ff44e4e6b92d0a2 Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Sun, 16 Jun 2024 16:15:37 +0530 Subject: [PATCH] Added Q608 --- .../lc-solutions/0600-0699/0608-Tree-Node.md | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0600-0699/0608-Tree-Node.md diff --git a/dsa-solutions/lc-solutions/0600-0699/0608-Tree-Node.md b/dsa-solutions/lc-solutions/0600-0699/0608-Tree-Node.md new file mode 100644 index 000000000..f4fb842f0 --- /dev/null +++ b/dsa-solutions/lc-solutions/0600-0699/0608-Tree-Node.md @@ -0,0 +1,101 @@ +--- +id: tree-node +title: Tree Node +sidebar_label: 0608-Tree-Node +tags: +- Database +description: "SQL Schema: Given a table with nodes, write an SQL query to find all nodes which are the only child nodes." +--- + +## Problem + +Given a table `Tree`, write an SQL query to find all nodes which are the only child nodes. + +The `Tree` table contains these columns: + +- `id`: integer +- `p_id`: integer (parent id) + +### Examples + +**Example:** + +**Input:** +Tree table: +``` ++----+------+ +| id | p_id | ++----+------+ +| 1 | null | +| 2 | 1 | +| 3 | 1 | +| 4 | 2 | +| 5 | 2 | +| 6 | 3 | +| 7 | 4 | +| 8 | 5 | ++----+------+ +``` +**Output:** +``` ++----+ +| id | ++----+ +| 6 | +| 7 | +| 8 | ++----+ +``` +Explanation: Nodes `6`, `7`, and `8` are the only child nodes. + +### Constraints + +- `id` is the primary key column for this table. +- Each row of this table contains information about the `id` of a node and the `id` of its parent node. + +--- + +## Approach + +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. + +### Steps: + +1. Count the number of children for each parent. +2. Select the nodes where the parent has exactly one child. + +### Solution + +#### SQL + +```sql +SELECT + id +FROM + Tree +WHERE + p_id IN ( + SELECT + p_id + FROM + Tree + GROUP BY + p_id + HAVING + COUNT(*) = 1 + ); +``` +### Explanation +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. + +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. + +### Complexity Analysis +**Time Complexity:** O(n log n) +>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). + +**Space Complexity:** O(n) +>Reason: The space complexity is mainly due to the storage of intermediate results, which can be linear in the size of the input. + +### References +**LeetCode Problem:** Tree Node \ No newline at end of file