Binary Search Tree - Hackerrank
Identifying Node Types in a Binary Search Tree Using SQL In this blog post, we will explore how to identify the types of nodes in a Binary Search Tree (BST) using SQL. The table BST contains two columns: N and P , where N represents the value of a node in the Binary Tree, and P is the parent of N . We aim to find the node type of each node, ordered by the value of the node. Problem Statement You are given a table, BST , containing two columns: N : Represents the value of a node in the Binary Tree. P : Represents the parent of N . You need to write a query to determine the node type of each node, ordered by the value of the node. The output should be one of the following for each node: Root: If the node is a root node. Leaf: If the node is a leaf node. Inner: If the node is neither a root nor a leaf node. Sample Input Sample Output 1 Leaf 2 Inner 3 Leaf 5 Root 6 Leaf 8 Inner 9 Leaf Explanation The Binary Tree below illustrates the sample: SQL Query to Solve the Problem SELECT ...