ocaml: phase 5.1 max_path_tree.ml baseline (max root-to-leaf sum, 1+3+7 = 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Recursive ADT for binary trees:
type tree = Leaf | Node of int * tree * tree
let rec max_path t =
match t with
| Leaf -> 0
| Node (v, l, r) ->
let lp = max_path l in
let rp = max_path r in
v + (if lp > rp then lp else rp)
For the test tree:
1
/ 2 3
/ \ \
4 5 7
paths sum: 1+2+4=7, 1+2+5=8, 1+3+7=11. max = 11.
Tests 3-arg Node constructor with positional arg destructuring, two
nested let-bindings, and if-then-else as an inline expression.
73 baseline programs total.
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
"flatten_tree.ml": 28,
|
||||
"list_ops.ml": 30,
|
||||
"mat_mul.ml": 621,
|
||||
"max_path_tree.ml": 11,
|
||||
"mod_inverse.ml": 27,
|
||||
"json_pretty.ml": 24,
|
||||
"kadane.ml": 6,
|
||||
|
||||
21
lib/ocaml/baseline/max_path_tree.ml
Normal file
21
lib/ocaml/baseline/max_path_tree.ml
Normal file
@@ -0,0 +1,21 @@
|
||||
type tree = Leaf | Node of int * tree * tree
|
||||
|
||||
let rec max_path t =
|
||||
match t with
|
||||
| Leaf -> 0
|
||||
| Node (v, l, r) ->
|
||||
let lp = max_path l in
|
||||
let rp = max_path r in
|
||||
v + (if lp > rp then lp else rp)
|
||||
|
||||
;;
|
||||
|
||||
let t = Node (1,
|
||||
Node (2,
|
||||
Node (4, Leaf, Leaf),
|
||||
Node (5, Leaf, Leaf)),
|
||||
Node (3,
|
||||
Leaf,
|
||||
Node (7, Leaf, Leaf)))
|
||||
in
|
||||
max_path t
|
||||
Reference in New Issue
Block a user