1
我想编写要列出的级别上的所有节点。OCaml - 解析树中级别的节点
type 'a tree = T of 'a * 'a tree * 'a tree;;
let at_level t lvl=
let rec aux t k =
match t with
|Leaf -> []
|T(x, l, r) -> if k = lvl then
(x::aux l (k+1)) @ (aux r (k+1))
else
(aux l (k+1)) @(aux r (k+1))
in aux t lvl;;
但我总是收到结果:[x]其中x是根的值。 我的程序中的问题在哪里?
提示:广度优先搜索 –