Suppose we defined a tree of integers :

type inttree = Int of int | Node of inttree * inttree ;;

is there any possible way to find the sum of the elements of that tree ?

有帮助吗?

解决方案

Try a simple recursive function (doing a depth first traversal) like

 let rec mysum t = match t with
      Int x -> x
    | Node (l,r) -> mysum l + mysum r
 ;;

The first line might be let rec mysum = function (it is a matter of style).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top