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.
As we are supposed to display specific text for each node:
Root: If node is root node.
Leaf: If node is leaf node.
Inner: If node is neither root nor leaf node.
in T-SQL we should use conditional CASE expression.
Hence, if the parent (P) of the node (N) is NULL we print 'Root' since it means that the node (N) doesn't have a parent. Else if a node is a parent node to any other nodes it means that such a node cannot be a leaf node, so it must be an inner node. Finally, if the above conditions weren't met it means, that the node (N) must be a leaf node, so we output 'Leaf'.
Obviously we order the result according to the column N.
Hope this answers your question.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Binary Tree Nodes
You are viewing a single comment's thread. Return to all comments →
As we are supposed to display specific text for each node:
in T-SQL we should use conditional CASE expression. Hence, if the parent (P) of the node (N) is NULL we print 'Root' since it means that the node (N) doesn't have a parent. Else if a node is a parent node to any other nodes it means that such a node cannot be a leaf node, so it must be an inner node. Finally, if the above conditions weren't met it means, that the node (N) must be a leaf node, so we output 'Leaf'. Obviously we order the result according to the column N. Hope this answers your question.