# Why the query to find the node type in Binary Tree does not work

I am working on the HackerRank practice – Binary Tree Nodes.

The table BST, contains two columns: N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.

It asks:

Write a query to find the node type of Binary Tree ordered by the value of the node. Output one of the following for each node:

`Root: If node is root node. Leaf: If node is leaf node. Inner: If node is neither root nor leaf node. `

Below query works:

`SELECT CASE WHEN P IS NULL THEN CONCAT(N, ' Root') WHEN N IN (SELECT DISTINCT P FROM BST) THEN CONCAT(N, ' Inner') ELSE CONCAT(N, ' Leaf') END FROM BST ORDER BY N; `

However, if I reverse the query a bit to get the Leaf node type 1st then it no longer works. Any thoughts? Thanks.

`SELECT CASE WHEN P IS NULL THEN CONCAT(N, ' Root') WHEN N NOT IN (SELECT DISTINCT P FROM BST) THEN CONCAT(N, ' Leaf') ELSE CONCAT(N, ' Inner') END FROM BST ORDER BY N; `