LeetCode 623. Add One Row to Tree in F#
URL
leetcode.com/problems/add-one-row-to-tree
Code
github.com/syohex/dotnet-study/blob/master/..
type Tree =
| Leaf
| Node of int * Tree * Tree
let addOneRow (root: Tree) (value: int) (depth: int) : Tree =
let rec addOneRow' node d value depth =
match node with
| Leaf -> Leaf
| Node (v, left, right) ->
if d + 1 = depth then
let newLeft = Node(value, left, Leaf)
let newRight = Node(value, Leaf, right)
Node(v, newLeft, newRight)
else
Node(v, addOneRow' left (d + 1) value depth, addOneRow' right (d + 1) value depth)
if depth = 1 then
Node(value, root, Leaf)
else
addOneRow' root 1 value depth