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.
""" Node is defined asclassnode:def__init__(self,data):self.data=dataself.left=Noneself.right=None"""# Could've used inorder traversal and, every time a node is added to results array, check if it's actually greater than the value prior to it, but decided to use try this approach instead, lol.defcheck_binary_search_tree_(root):# node, min val in current range, max val in current range. Range updates every time a new node is visited. Given the range values, I can implicitly tell whether I'm moving either to the left or to the right.stack=[(root,0,10e4)]whilestack:current,min_in_range,max_in_range=stack.pop()ifnot(min_in_range<current.data<max_in_range):returnFalseifcurrent.right:stack.append((current.right,current.data,max_in_range))ifcurrent.left:stack.append((current.left,min_in_range,current.data))returnTrue
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Is This a Binary Search Tree?
You are viewing a single comment's thread. Return to all comments →