Created
January 22, 2016 02:45
-
-
Save jwhiteman/90a3e37b9f55ad07eca7 to your computer and use it in GitHub Desktop.
invert a binary tree in Elixir
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
# http://is.gd/NzZxYT | |
defmodule BinaryTree do | |
def invert([n, list]) when is_integer(n) and is_list(list) do | |
[n, invert(list)] | |
end | |
def invert([n, nc, m, mc]) | |
when is_integer(n) and is_integer(m) | |
and is_list(nc) and is_list(mc) do | |
[m, invert(mc), n, invert(nc)] | |
end | |
def invert([n, m]) when is_integer(n) and is_integer(m) do | |
[m, n] | |
end | |
def invert([n]) when is_integer(n) do | |
[n] | |
end | |
def invert([]) do | |
[] | |
end | |
end | |
BinaryTree.invert([4, [2, [1, 3], 7, [6, 9]]]) | |
# => [4, [7, [9, 6], 2, [3, 1]]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment