You are viewing a single comment's thread. Return to all 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
Seems like cookies are disabled on this browser, please enable them to open this website
Trees: Is This a Binary Search Tree?
You are viewing a single comment's thread. Return to all 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.
Three possible Python 3 Solutions