Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree in F#

Shohei Yoshida's photo
Shohei Yoshida
·May 17, 2022·

1 min read

This problem is not suit for F#. So I wrote modified version.

URL

leetcode.com/problems/find-a-corresponding-..

Code

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

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

let rec getTargetCopy (original: Tree) (cloned: Tree) (target: int) : Tree =
    match original, cloned with
    | Leaf, Leaf -> Leaf
    | Node (v1, left1, right1), Node (_, left2, right2) ->
        if v1 = target then
            cloned
        else
            match getTargetCopy left1 left2 target with
            | Node (_, _, _) as r -> r
            | Leaf -> getTargetCopy right1 right2 target
    | _, _ -> failwith "never reach here"
 
Share this