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
|
2162 Discussions
|
Please Login in order to post a comment
"_ first select the node then for each node check weather their parent is null if parent of a node is null, it means that particular node is a 'Root Node' if for any node their parent is not null then we'll check that particular node is paresent in parent section , if yes it means that node will be a 'Inner node' Rest of the else node will be "leaf node" because those are not parent of any node.
SELECT N, CASE WHEN P IS NULL THEN 'Root' WHEN N IN (SELECT DISTINCT P FROM BST) THEN 'Inner' ELSE 'Leaf' END AS Node_Type FROM BST ORDER BY N;
/
FOR MS SQL SERVER
select N ,Case when P is null then 'Root' when N in (select P from BST) then 'Inner' else 'Leaf' END as value from BST order by N
Hope this helps.