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.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Tutorials
  3. Cracking the Coding Interview
  4. Trees: Is This a Binary Search Tree?
  5. Discussions

Trees: Is This a Binary Search Tree?

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • talkdirty 3 years ago+ 0 comments

    Here's one way to create the Node Data Structure from Input, in case anyone wants to play with this challenge outside of the Hackerrank Platform.

    class Node:
        def __init__(self, data, left=None, right=None):
            self.data = data
            self.left = left
            self.right = right
    
    def make_perfect_btree_node(heap, lower, higher, depth):
        target_idx = int((higher + lower)/ 2)
        if depth == 0:
            return None
        return Node(int(heap[target_idx]),
                   make_perfect_btree_node(heap, lower, target_idx, depth - 1),
                   make_perfect_btree_node(heap, target_idx, higher, depth - 1))
    
    def construct_perfect_btree_from_input(stream):
        depth = int(stream.readline())
        heap = stream.readline().split()
        return make_perfect_btree_node(heap, 0, len(heap), depth + 1)
    		
    construct_perfect_btree_from_input(StringIO('''2
    1 2 3 4 5 6 7'''))
    

    Three possible Python 3 Solutions

    0|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature