Skip to content

Instantly share code, notes, and snippets.

@amilie
Created May 5, 2017 19:27
Show Gist options
  • Save amilie/359b6240b448d9a84e069dce28b49ce0 to your computer and use it in GitHub Desktop.
Save amilie/359b6240b448d9a84e069dce28b49ce0 to your computer and use it in GitHub Desktop.
My solution to the BST challenge
-- https://www.hackerrank.com/challenges/binary-search-tree-1
select node, type from (
(select N as node, "Root" as type from BST where P is null)
union
(select t1.N as node, "Leaf" as type from BST t1
left join BST t2 on t1.N = t2.P
where t2.P is null)
union
(select distinct t1.N as node, "Inner" as type from BST t1
left join BST t2 on t1.N = t2.P
where t1.P is not null and t2.P is not null)
) as U order by node;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment