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.
- Prepare
- SQL
- Advanced Select
- Binary Tree Nodes
- Discussions
Binary Tree Nodes
Binary Tree Nodes
Sort by
recency
|
2356 Discussions
|
Please Login in order to post a comment
MS SQL SERVER left join
select N , case when P IS NULL then "Root" when N in (select p from BST where p is not null) then "Inner" else "Leaf" end as types from BST order by n
MySQL / PostgreSQL
select N, case when P is null then 'Root' when N not in (select P from BST where P is not null) then 'Leaf' else 'Inner' end as Type from BST order by N;
select * from (
SELECT CAST(A.N AS VARCHAR) +' Leaf' as n FROM ( SELECT N FROM BST EXCEPT SELECT P FROM BST ) AS A
UNION
SELECT CAST(N AS VARCHAR) +' Root' as n FROM BST WHERE P IS NULL
UNION
SELECT CAST(B.N AS VARCHAR) +' Inner' as n FROM (
SELECT N FROM BST WHERE P IS not NULL INTERSECT SELECT P FROM BST
) AS B ) as X order by cast(left(x.n,2) as int);