Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 94. Binary Tree Inorder Traversal in F#

Shohei Yoshida's photo
Shohei Yoshida
·Sep 8, 2022·

1 min read

URL

leetcode.com/problems/binary-tree-inorder-t..

Code

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

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

let inorderTraversal (root: Tree) : int list =
    let rec inorderTraversal' node acc =
        match node with
        | Leaf -> acc
        | Node (v, left, right) ->
            let acc' = inorderTraversal' left acc
            let acc'' = v :: acc'
            inorderTraversal' right acc''

    inorderTraversal' root [] |> List.rev
 
Share this