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
|
2483 Discussions
|
Please Login in order to post a comment
with cte_tree as ( select distinct a.n as node from bst a join bst b on a.n=b.p )select (case when p is null and n in (select * from cte_tree) then concat(convert(varchar(4),n),' Root') when n in (select * from cte_tree) then concat(convert(varchar(4),n),' Inner') else concat(convert(varchar(4),n),' Leaf') end) as num from bst order by n
My SQL:
SELECT Distinct N, CASE WHEN P is null THEN 'Root' WHEN c.P is null THEN 'Leaf' ELSE 'Inner' END as Value FROM (SELECT A.N as N,B.P as P,A.P as root FROM BST A LEFT OUTER JOIN BST B ON A.N=B.P)as c ORDER BY N
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
Okay, so i have a major issue with this sim, it keeps returning wrong code for capitalization? WTH? my code was right but it returned 'leaf' instead of 'Leaf' and it kept erroring out.