[back]

Height of a Binary Tree

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

The height of a binary tree is the length of the longest path from the root to a leaf (in this case, to any Empty constructor).

Implement the function

val height : 'a tree -> int

so that height t is the height of t.

Examples

let _ = assert (height Empty = 0)
let _ = assert (height (Node (1, Empty, Empty)) = 1)

let t1 = Node (1, Node (2, Empty, Empty), Node (3, Empty, Empty))
let _ = assert (height t1 = 2)

let t2 = Node (1, t1, Empty)
let _ = assert (height t2 = 3)