mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 18:38:03 +00:00
Polish bst challenge and solution (#75)
Update constraints, algorithm discussion, and code.
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user