-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrees-binary-search-tree.py
45 lines (35 loc) · 1.11 KB
/
trees-binary-search-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# https://www.hackerrank.com/challenges/ctci-is-binary-search-tree/problem
# Node is defined as
class node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
nodes = []
def inOrderTraversal(node):
if node is None:
return
inOrderTraversal(node.left)
nodes.append(node.data)
inOrderTraversal(node.right)
def right(root, pdata):
if (root is None):
return True
if (root.data <= pdata):
return False
return right(root.right, root.data) and right(root.right, pdata) \
and right(root.left, pdata) and left(root.left, root.data)
def left(root, pdata):
if (root is None):
return True
if (root.data >= pdata):
return False
return left(root.left, root.data) and left(root.left, pdata) \
and left(root.right, pdata) and right(root.right, root.data)
def checkBST(root):
return left(root.left, root.data) and right(root.right, root.data)
# inOrderTraversal(root)
# for i in range(1, len(nodes)):
# if nodes[i] <= nodes[i-1]:
# return False
# return True