Wednesday, May 24, 2017

SQL advanced select - Binary Tree Nodes

 Problem
You are given a table, BST, containing two columns: and P, where N represents the value of a node in Binary Tree, and P is the parent of N.

Write a query to find the node type of Binary Tree ordered by the value of the node. Output one of the following for each node:
  • Root: If node is root node.
  • Leaf: If node is leaf node.
  • Inner: If node is neither root nor 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:


Answer
 SELECT n,
       IF(p IS NULL, 'Root', IF((SELECT Count(*)
                                 FROM   bst
                                 WHERE  p = B.n) > 0, 'Inner', 'Leaf'))
FROM   bst AS B
ORDER  BY n 

1 comment:

  1. SELECT n,
    CASE
    WHEN p IS NULL THEN 'Root'
    WHEN n IN (SELECT p
    FROM bst) THEN 'Inner'
    ELSE 'Leaf'
    END AS Node
    FROM bst
    ORDER BY n;

    ReplyDelete