list of the values in the leaf nodes of binary tree T

后端 未结 3 1727
梦如初夏
梦如初夏 2020-11-28 15:54

List is the list of values in leaf nodes of a binary tree and I am trying to figure out how to output just that. This is giving me all the nodes but I need just the leaves

3条回答
  •  生来不讨喜
    2020-11-28 16:35

    The same solution, not so far from first implementation, can be expressed as:

    lea(nil, []).
    lea(t(X, nil, nil), [X]).
    lea(t(_, A, B), L) :-
        lea(A, L1),
        lea(B, L2),
        append(L1, L2, L)
        L \= [].
    

    The last row (L \= []) can be removed (if you accept the possibility of find every solution).

提交回复
热议问题