LeetCode 1372. Longest ZigZag Path in a Binary Tree in F#
URL
Longest ZigZag Path in a Binary Tree - LeetCode
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/1372/main.fsx
open System
type Tree =
| Leaf
| Node of int * Tree * Tree
let longestZigZa (root: Tree) : int =
let rec longestZigZag' node depth goLeft =
match node with
| Leaf -> depth - 1
| Node(_, left, right) ->
if goLeft then
Math.Max(longestZigZag' left (depth + 1) false, longestZigZag' right 1 true)
else
Math.Max(longestZigZag' right (depth + 1) true, longestZigZag' left 1 false)
Math.Max(longestZigZag' root 0 true, longestZigZag' root 0 false)