LeetCode 1367. Linked List in Binary Tree in F#

URL

https://leetcode.com/problems/linked-list-in-binary-tree/description/?envType=daily-question&envId=2024-09-07

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202409/linked_list_in_binary_tree/main.fsx

type MyList =
    | Nil
    | ListNode of int * MyList

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

let rec isSubPath (head: MyList) (root: Tree) : bool =
    let rec dfs list node =
        match list with
        | Nil -> true
        | ListNode(v1, next) ->
            match node with
            | Leaf -> false
            | TreeNode(v2, left, right) -> if v1 = v2 then dfs next left || dfs next right else false

    match root with
    | Leaf -> false
    | TreeNode(_, left, right) -> dfs head root || isSubPath head left || isSubPath head right