[back]

Flattening Binary Trees

type 'a tree
  = Empty
  | Node of 'a * 'a tree * 'a tree

Implement the function

val flatten : 'a tree -> 'a list

so that flatten t is the list with the same elements as t in-order.

Example

The function insert is implemented a previous problem.

let _ = assert (flatten (insert 4 (insert 8 (insert 3 (insert 5 Empty)))) = [3; 4; 5; 8])