Polish bst challenge and solution (#75)

Update constraints, algorithm discussion, and code.
This commit is contained in:
Donne Martin
2016-06-24 07:23:20 -04:00
committed by GitHub
parent c7e9a85db7
commit c24a628329
3 changed files with 30 additions and 20 deletions

View File

@@ -11,20 +11,19 @@ class Node(object):
def insert(root, data):
# Constraint: Assume we are working with valid ints
if root is None:
root = Node(data)
return root
if data <= root.data:
if root.left is None:
root.left = Node(data)
root.left = insert(root.left, data)
root.left.parent = root
return root.left
else:
return insert(root.left, data)
insert(root.left, data)
else:
if root.right is None:
root.right = Node(data)
root.right = insert(root.right, data)
root.right.parent = root
return root.right
else:
return insert(root.right, data)
insert(root.right, data)