Skip to content

Instantly share code, notes, and snippets.

@jwhiteman
Created January 22, 2016 02:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwhiteman/90a3e37b9f55ad07eca7 to your computer and use it in GitHub Desktop.
Save jwhiteman/90a3e37b9f55ad07eca7 to your computer and use it in GitHub Desktop.
invert a binary tree in Elixir
# 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