title: Classifying binary tree nodes using SQL
body: <p>I am trying to solve the problem mentioned at the below link</p>
  
  <p><a href="https://www.hackerrank.com/challenges/binary-search-tree-1/problem" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/binary-search-tree-1/problem</a></p>
  
  <p>I have written the following code.Please help me where i am going wrong</p>
  
  <pre><code>Select q.Node,case
                WHEN q.Node NOT IN q.Parent THEN 'Leaf' 
                WHEN q.Node IN q.Parent AND q.Node NOT IN q.Higher_Parent THEN 'Inner'
                WHEN q.Node IN q.Parent AND q.Node IN q.Higher_Parent THEN 'Root'
                END as NodeType
  from (
  SELECT B1.N as Node,B1.P as Parent,B2.P as Higher_Parent FROM 
  BST B1 INNER JOIN BST B2 
  ON B1.P = B2.N
  ORDER BY Node ASC
  ) q
  
  N P HP
  1 2 5
  2 5 NULL
  3 2 5
  6 8 5
  8 5 NULL
  9 8 5
  </code></pre>
  
  <p>Where should i modify the above code to work. Sure there are other concise codes for the same problem but please help me with this code for learning purpose. </p>