You are viewing a single comment's thread. Return to all comments →
I found 3 solutions:
Method 1
SELECT N, CASE WHEN P is NULL Then 'Root' WHEN N NOT IN (SELECT B1.P FROM BST B1 JOIN BST B2 ON B1.P=B2.N ) Then 'Leaf' ELSE 'Inner' END AS Node FROM BST ORDER BY N
Method 2:
SELECT N, CASE WHEN P IS NULL THEN 'Root' WHEN N IN (SELECT P FROM BST WHERE P IS NOT NULL) THEN 'Inner' ELSE 'Leaf' END AS Node FROM BST ORDER BY N;
Method 3:
SELECT N, CASE WHEN P IS NULL THEN 'Root' WHEN N NOT IN (SELECT P FROM BST WHERE P IS NOT NULL) THEN 'Leaf' ELSE 'Inner' END AS Node FROM BST ORDER BY N;
The key is to specify 'P IS NOT NULL' so as to separate the Leaf and Inner nodes.
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 →
I found 3 solutions:
Method 1
Method 2:
Method 3:
The key is to specify 'P IS NOT NULL' so as to separate the Leaf and Inner nodes.