문제

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