Created
June 2, 2015 10:04
-
-
Save pocha/80fce088aa3ab7a9c9f1 to your computer and use it in GitHub Desktop.
Mirror Binary Tree without recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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