We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
select b.N,
case when b.P is null then 'Root'
when b2.P IS NULL THEN 'Leaf'
else 'Inner' end
from BST as b left join (select P from parents ) as b2 on b.N = b2.P order by b.N
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Binary Tree Nodes
You are viewing a single comment's thread. Return to all comments →
MS SQL - With join
WITH Parents AS ( SELECT DISTINCT P FROM BST )
select b.N, case when b.P is null then 'Root' when b2.P IS NULL THEN 'Leaf' else 'Inner' end from BST as b left join (select P from parents ) as b2 on b.N = b2.P order by b.N