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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. SQL
  3. Advanced Select
  4. Binary Tree Nodes
  5. Discussions

Binary Tree Nodes

Problem
Submissions
Leaderboard
Discussions

Sort 1591 Discussions, By:

recency

Please Login in order to post a comment

  • giddi2452
    1 day ago+ 0 comments

    Approach: If parent p is null it is ROOT, For INNER parent P should not be null and child should be there, so number in Parent column P have a child so check if N number node present Parent p column then Inner

    select N, CASE when P is null then "Root" when P is not null and N in (select distinct P from BST) then "Inner" else "Leaf" END as type_node FROM BST
    order by N ASC

    0|
    Permalink
  • moseshvilidato
    3 days ago+ 0 comments

    it works in oracle

    select b.N, case when b.p is null then 'Root' when b.N in (select k.p from bst k where k.p is not null) then 'Inner' else 'Leaf' end from bst b order by 1;

    0|
    Permalink
  • varun_hkr
    5 days ago+ 0 comments

    Using IF()

    select bst.N,
    IF(bst.p IS NULL,'Root',  IF(bst.n in (select bst.n from bst where bst.p is not null and n in (select p from bst)), 'Inner', 'Leaf')) 
    from bst
    order by bst.N
    
    0|
    Permalink
  • pavel_yanachkin
    6 days ago+ 0 comments

    select distinct b1.n, case when b2.p is null then 'Leaf' when b1.p is null then 'Root' else 'Inner' end as n_type from BST as b1 left join BST as b2 on b1.n = b2.p order by b1.n

    0|
    Permalink
  • nhipham117
    7 days ago+ 2 comments

    MS SQL Sever

    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
    
    -1|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy