type 'a tree = Leaf of 'a | Node of 'a tree list

let rec flatten t =
  match t with
  | Leaf x -> [x]
  | Node ts -> List.concat (List.map flatten ts)

;;

let t = Node [
  Leaf 1;
  Node [Leaf 2; Leaf 3];
  Node [Node [Leaf 4]; Leaf 5; Leaf 6];
  Leaf 7
]
in
List.fold_left (+) 0 (flatten t)
