type tree = Leaf | Node of int * tree * tree

let rec max_path t =
  match t with
  | Leaf -> 0
  | Node (v, l, r) ->
    let lp = max_path l in
    let rp = max_path r in
    v + (if lp > rp then lp else rp)

;;

let t = Node (1,
  Node (2,
    Node (4, Leaf, Leaf),
    Node (5, Leaf, Leaf)),
  Node (3,
    Leaf,
    Node (7, Leaf, Leaf)))
in
max_path t
