Skip to content

Instantly share code, notes, and snippets.

@tomelm
Last active December 25, 2015 06:49
Show Gist options
  • Save tomelm/6934377 to your computer and use it in GitHub Desktop.
Save tomelm/6934377 to your computer and use it in GitHub Desktop.
class Node
attr_accessor :value, :left, :right
end
def subtree(root, min, max)
if root.value >= min
if root.left <= min
if root.left.right >= min
root.left = root.left.right
else
root.left = null
end
end
end
if root.value <= max
if root.right >= max
if root.right.left <= max
root.right = root.right.left
else
root.right = null
end
end
end
subtree(root.left, min, max) if root.left
subtree(root.right, min, max) if root.right
end
10
/ \
8 14
/ \ \
7 9 16
def subtree(root, min, max)
if root.left <= max
root.left = subtree(root.left, min, max)
end
if root.right >= min
root.right = subtree(root.right, min, max)
end
if root.left == null && root.right == null && root.value >= min && root.value <= max
return root
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment