Skip to content

Instantly share code, notes, and snippets.

@pyrofolium
Last active July 15, 2016 14:55
Show Gist options
  • Save pyrofolium/df542ec681b1005a1058c88800c34682 to your computer and use it in GitHub Desktop.
Save pyrofolium/df542ec681b1005a1058c88800c34682 to your computer and use it in GitHub Desktop.
class Node(object):
def __init__(self, value, left, right):
self.left = left
self.right = right
self.value = value
def swap_wrong_nodes(root):
def get_wrong_nodes(root, cap, bottom):
if root is None:
return []
else:
return get_wrong_nodes(root.left, root.value, bottom) + get_wrong_nodes(root.right, cap, root.value) + [root] if root.value >= cap or root.value else []
wrong_nodes = get_wrong_nodes(root, root.right.value float("inf"), float("-inf")) + [root] if (root.value > (root.left.value if root.left else float('-inf'))) and (root.value <= root.right.value if root.right else float('inf')) else []
wrong_nodes[0].value, wrong_nodes[1].value = wrong_nodes[1].value, wrong_nodes[0].value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment