Skip to content

Instantly share code, notes, and snippets.

@pocha
Created June 2, 2015 10:04
Show Gist options
  • Save pocha/80fce088aa3ab7a9c9f1 to your computer and use it in GitHub Desktop.
Save pocha/80fce088aa3ab7a9c9f1 to your computer and use it in GitHub Desktop.
Mirror Binary Tree without recursion
def mirror(p)
while switched(p) == false
if switched(p.left) and switched(p.right)
right = p.right
p.right = p.left
p.left = right
right = nil
p.switched = true
p = p.parent
end
if !switched(p.left)
p = p.left
end
if !switched(p.right)
p = p.right
end
end
end
def switched(p)
return true if p.nil?
if p.left.nil? and p.right.nil?
return true
end
return p.switched
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment