我有一个Ocaml示例,我正在处理它。我需要根据从左到深的原则来显示某种树中所有分支的长度。所以如果我们有这棵树:
4
2 6
1 3 5 7我想在树上显示所有树枝的长度。你有什么想法吗?
我们有衬套的类型,它的定义如下:
# type 'a bush =
Null
| One of 'a * 'a bush
| Two of 'a bush * 'a * 'a bush;;写一个函数“分支长度”‘一个灌木丛的->单元,它根据从左到深的原理显示一个灌木丛的分支的长度!
发布于 2013-06-20 16:51:49
根据你对分支长度的定义,你可以这样递归地遍历你的树:
let rec branches_length bush depth = match bush with
| Null -> [depth]
| One (a, b) -> (branches_length b (depth + 1))
| Two (a, b, c) -> (branches_length a (depth + 1))@(branches_length c (depth + 1))https://stackoverflow.com/questions/17196781
复制相似问题