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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. SQL
  3. Advanced Select
  4. Binary Tree Nodes
  5. Discussions

Binary Tree Nodes

Problem
Submissions
Leaderboard
Discussions

Sort 1278 Discussions, By:

votes

Please Login in order to post a comment

  • rahmatNazali
    5 years ago+ 54 comments

    A simple solution using MySQL Case. I hope this helps.

    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 ASC
    
    291|
    Permalink
    View more Comments..
  • tubededentifrice
    7 years ago+ 48 comments

    Easier is better SELECT N, IF(P IS NULL,'Root',IF((SELECT COUNT(*) FROM BST WHERE P=B.N)>0,'Inner','Leaf')) FROM BST AS B ORDER BY N;

    167|
    Permalink
    View more Comments..
  • cheftr
    6 years ago+ 5 comments

    Worked for me:

    select n, case when (p is null) then 'Root' when (n in (select p from bst)) then 'Inner'else 'Leaf' end from bst order by n;

    30|
    Permalink
    View more Comments..
  • marinskiy
    4 years ago+ 4 comments

    Here is Oracle solution from my HackerrankPractice repository:

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

    Feel free to ask if you have any questions :)

    24|
    Permalink
    View more Comments..
  • defiant_dj04
    4 years ago+ 3 comments

    Solution 1: Oracle/MySQL/MS SQL Server

    SELECT n,
    	CASE
    		WHEN p IS NULL THEN 'Root'
    		WHEN n IN (SELECT DISTINCT p FROM bst) THEN 'Inner'
    		ELSE 'Leaf'
    	END
    FROM bst
    ORDER BY n;
    

    Solution 2: Oracle/MySQL/MS SQL Server

    SELECT n,
    	CASE
    		WHEN p IS NULL THEN 'Root'
    		WHEN EXISTS (SELECT * FROM bst in_bst WHERE in_bst.p = out_bst.n) THEN 'Inner'
    		ELSE 'Leaf'
    	END
    FROM bst out_bst
    ORDER BY n;
    

    Solution 3: Oracle/MySQL/MS SQL Server

    SELECT n,
    	CASE
    		WHEN p IS NULL THEN 'Root'
    		WHEN ((SELECT COUNT(*) FROM bst in_bst WHERE in_bst.p = out_bst.n) > 0) THEN 'Inner'
    		ELSE 'Leaf'
    	END
    FROM bst out_bst
    ORDER BY n;
    

    Solution 4: Oracle/MySQL/MS SQL Server

    SELECT n,
    	CASE
    		WHEN p IS NULL THEN 'Root'
    		WHEN n IN (SELECT bst_1.p FROM bst bst_1 JOIN bst bst_2 ON bst_1.p = bst_2.n) THEN 'Inner'
    		ELSE 'Leaf'
    	END
    FROM bst
    ORDER BY n;
    

    Solution 5: MySQL

    SELECT n, IF(p IS NULL, 'Root', IF((SELECT COUNT(*) FROM bst in_bst WHERE in_bst.p = out_bst.n) > 0, 'Inner', 'Leaf')) 
    FROM bst out_bst
    ORDER BY n;
    
    16|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature