LeetCode 606. Construct String from Binary Tree in F#

URL

leetcode.com/problems/construct-string-from..

Code

github.com/syohex/dotnet-study/blob/master/..

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

let tree2str (root: Tree) : string =
    let rec tree2str' node : string =
        match node with
        | Leaf -> ""
        | Node (v, left, right) ->
            let mutable ret = string v
            let tmp1 = tree2str' left
            let tmp2 = tree2str' right

            if tmp1.Length <> 0 then
                ret <- ret + "(" + tmp1 + ")"

            if tmp2.Length <> 0 then
                if tmp1.Length = 0 then
                    ret <- ret + "()"

                ret <- ret + "(" + tmp2 + ")"

            ret

    tree2str' root