我在解决黑客队伍二叉树question.PFB的问题
给出了一个表BST,包含两个列: N和P,其中N表示二叉树中节点的值,P是N的父列。
编写查询,查找按节点值排序的二叉树节点类型。为每个节点输出以下内容之一:
Root: If node is root node.
Leaf: If node is leaf node.
Inner: If node is neither root nor leaf node.我能够使用下面的查询来解决这个问题
select n,
 case 
 when p is null then 'Root'
 when p is not null and (n in (select p from BST)) then 'Inner' else 'Leaf'
end
from BST order by n但在此之前,我尝试了下面的查询,但它不起作用
select n,
 case 
 when p is null then 'Root'
 when p is not null and (n in (select p from BST)) then 'Inner'
 when p is not null and (n not in (select p from BST)) then 'Leaf'
end
from BST order by n上面的查询给出了根注释和内部节点,但是它不是叶,而是空的,请有人解释一下它为什么会显示这种行为。
你可以试试这里这个问题
谢谢你
发布于 2020-09-09 17:42:38
因此,与其使用“NULL”和“和”,不如尝试一下..。
选择N,当B.p为NULL时,当B.N IN(从BST中选择p),然后从BST B顺序中选择‘内部’否则‘叶子’结束的情况下,从BST B顺序N;
https://stackoverflow.com/questions/50265704
复制相似问题