Loading
Lesson 46
Courses / HackerRank SQL Problem Solving Solution Challenges
Binary Tree Nodes | SQL Advanced Select | HackerRank Solution

Summary

Summary of Binary Tree Nodes SQL Problem

Overview

The task involves analyzing a binary tree represented by a dataset containing two columns: n (node values) and p (parent node values). The goal is to categorize each node as either a "root," "leaf," or "inner" node based on its relationship with parent nodes.

Node Definitions

  • Root Node: The top node which has no parent (i.e., p is null).
  • Leaf Node: A node with no children (not a parent to any other node).
  • Inner Node: A node that has at least one child (i.e., is a parent to another node).

Procedure to Identify Node Types

  1. Identify Root Nodes:

    • Check if p is null. If true, classify the node as a root.
  2. Identify Leaf Nodes:

    • Create a list of parent values (p) from the dataset excluding null values (i.e., non-parent nodes).
    • For each node value (n), check if it exists in the list of parents:
      • If it does not exist, classify the node as a leaf.
      • If it does exist, classify it as an inner node.
  3. SQL Implementation:

    • Use a CASE statement to streamline the classification based on the described conditions.
    • Final output should be ordered by node value (n).

SQL Code Outline

SELECT 
    n,
    CASE 
        WHEN p IS NULL THEN 'root'
        WHEN n IN (SELECT DISTINCT p FROM BST WHERE p IS NOT NULL) THEN 'inner'
        ELSE 'leaf'
    END AS description
FROM BST
ORDER BY n;

Conclusion

This SQL query compiles a necessary description of each node in a binary tree from the provided dataset, enabling effective understanding and manipulation of the tree structure.

Video Transcript

Hey everybody, I would do binary tree nodes from the advanced select section of SQL and HackerRank. So we're given the stable of the columns n and p. n is the value that's an integer, for example, one, two, or three. And p is the parent of that value. If we scroll down and see the diagram, this is what it looks like. So we have nodes, those circles, and they're connected. So that means a node can have children. In this case, five has two and eight as children. And then at the end, we have nodes that have no children. There's a call to leave nodes. And the very first node is called the root node at the top here in this diagram. So we're asked to compile this list of descriptions for each value for each node. For example, the value one, node of the value one, is it a leaf, a neater or root? In this case, it is a leaf. Because if you look at the diagram, one has no children. Okay, so let me show you side by side. These are the rows. So first row, n one has the parent two. It's better that you look at the parent first to make sense of what it looks like visually. So let's look at all the values for p. So we have two, we have eight, and we have five. So that means that two is a parent to one, and two is also parent to three. In the graph here on the right hand side, that means that two is a parent to one, you can see here. And two is a parent to three. So two has the children one and three. And if you go on eight is a parent to six, and eight is a parent to nine. So that means eight has the children six and nine. And then finally, five is a parent to two, and a parent to eight. So five is the parent of two, and the parent of eight. So that's what it looks like visually. So we are asked to describe each of these nodes, whether they are root, the very first of the top, whether they are leaves, the in this diagram, they're at the bottom usually, and they have no children or their inner, meaning they have both a parent and a child. Now getting back, let's choose my sequel. I'm going to do a select star from VST. Let's look at the very first case root. So we want to display the value that's and followed by the description. Now the description, I'm going to use a case here. Let me split this into multiple lines so you can better see. So after that, I'm going to use a case. And for root, that's the easy one because what is a root has no parent. If you find the example here, the very last row is five, the Pb no. So if the root has no parents, the value of P is null. So here we're going to say when P is null, then root. So if it's not a root, that means it's either in between or someone at the bottom that isn't a parent to anyone. So I'm going to focus on the P value here. So if I do a select for the P values in a compile list of values for P that are non-null, we can see two, eight and five. And then if I take the value of N and check, hey, is this value of N contained in this list of parents, if it's not in the list of parents, that means it's a leaf, right? It's not apparent to any node. Otherwise, if it's apparent to any of these nodes, it's got to be inner. So let's do a here when the value of N in some list, I want to take the values of P and group them. If I do a select P from BST here, I'm going to get the values of P, but they can be repeated. So what I want to do is I want to group by P so that I eliminate any duplicates. So I get all the parents. I also want to get rid of null. So if I want to get rid of null, I can add a where here. P is not null. Now this will give us a list of parents, right? In this case, for this example, two, eight and five. So we're going to check is N in this list. If that is the case, then that means it's inner because N is apparent, right? It was in the list of parents. Otherwise else case, we did not find N in the list of parents. Therefore, it does not have any children. It is a leaf node. We're going to say leaf. Okay, so first we check if it's root, if that's the case, the parent is no. Okay, if it's not rude, that means, okay, I'm going to check the list of parents to see if that value is there. If that is the case, that means that value is apparent to someone, therefore it's inner. But if it's not apparent to anyone, it's got to be a leaf, the else case. Okay, just to finalize, I have to order by the value of the node, that is the value of N. So here at the end, I'm going to add order by N. Let's go submit code.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: