LeetCode 951. Flip Equivalent Binary Trees in F#

URL

https://leetcode.com/problems/flip-equivalent-binary-trees/description/?envType=daily-question&envId=2024-10-24

Code

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

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

let flipEquiv (root1: Tree) (root2: Tree) : bool =
    let rec flipEquiv' t1 t2 =
        match t1, t2 with
        | Leaf, Leaf -> true
        | Node _, Leaf
        | Leaf, Node _ -> false
        | Node(v1, left1, right1), Node(v2, left2, right2) ->
            if v1 <> v2 then
                false
            else
                (flipEquiv' left1 left2 && flipEquiv' right1 right2)
                || (flipEquiv' left1 right2 && flipEquiv' right1 left2)

    flipEquiv' root1 root2