Skip to content

Instantly share code, notes, and snippets.

@alinpopa
Created January 15, 2017 19:38
Show Gist options
  • Save alinpopa/c86f5186ef9a0be152d2668f4868c2db to your computer and use it in GitHub Desktop.
Save alinpopa/c86f5186ef9a0be152d2668f4868c2db to your computer and use it in GitHub Desktop.
type 'a tree =
| Leaf of 'a
| Node of 'a * 'a tree * 'a tree;;
let t = Node (6, (Node (7, (Leaf 1), (Leaf 2))), (Node (8, (Leaf 3), (Leaf 4))));;
let rec parse t =
let rec parse_trees trees acc = match trees with
| [] -> acc
| (Leaf i) :: tl -> parse_trees tl (i + acc)
| (Node (i, l, r)) :: tl -> parse_trees (l :: r :: tl) (acc + i)
in
parse_trees [t] 0;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment