Shohei Yoshida
Shohei Yoshida's Blog

Follow

Shohei Yoshida's Blog

Follow

LeetCode 100. Same Tree in F#

Shohei Yoshida's photo
Shohei Yoshida
·Jan 10, 2023·

1 min read

URL

Same Tree - LeetCode

Code

dotnet-study/main.fsx at master · syohex/dotnet-study · GitHub

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

let rec isSameTree (p: Tree) (q: Tree) : bool =
    match p, q with
    | Leaf, Leaf -> true
    | Node(_), Leaf
    | Leaf, Node(_) -> false
    | Node(v1, left1, right1), Node(v2, left2, right2) ->
        if v1 <> v2 then
            false
        else
            isSameTree left1 left2 && isSameTree right1 right2
 
Share this