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
|
2052 Discussions
|
Please Login in order to post a comment
WHEN I TRY SAME THING IN POSTGRESSQL I GOT RESULT NOT SURE WHY OTHER DBS ARE NOT SUPPORTING FYI..
select N AS NODE, case when P IS NULL THEN 'ROOT' when N IN (select distinct P from bst ) THEN 'LEAF' else 'Inner' end as nodeType from bst;
1 "Inner" 3 "Inner" 6 "Inner" 9 "Inner" 2 "LEAF" 8 "LEAF" 5 "ROOT"
Oracle select n, case
when p is NULL then 'Root' when n in (select p from bst) then 'Inner' when n !=any (select p from bst) then 'Leaf' end statu from bst order by n;
I Just share my solution:
select b1.N, case when count(b2.P) > 0 and b1.P is null then 'Root' when count(b2.P) > 0 and b1.P > 0 then 'Inner' when count(b2.P) = 0 then 'Leaf' end from BST b1 left join BST b2 on b1.N = b2.P group by b1.N,b1.P order by b1.N asc