class Node: def __init__(self, data): self.data = data self.left = None self.right = None sorted_array=[] # def isBST(node): # return True def isBST(node): print node.data if node.left != None and node.left.data <= node.data: isBST(node.left) if node == None: return sorted_array.append(node.data) if node.right != None and node.right.data >= node.data: isBST(node.right) # Driver program to test above function #positive data # root = Node(4) # root.left = Node(2) # root.right = Node(5) # root.left.left = Node(1) # root.left.right = Node(3) #Negative data root = Node(3) root.left = Node(2) root.right = Node(5) root.left.left = Node(1) root.left.right = Node(4) def isSorted(lst): return all(x <= y for x,y in zip(lst, lst[1:])) isBST(root) #sorted_array=[1,5,3,4] print sorted_array if(isSorted(sorted_array)): print 'Yo its BST' else: print 'No its not'
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question