August 19, 2024

P61A - Collect the leaves of a binary tree in a list

A leaf is a node with no successors. Write a function leaves to return them in a list.

(leaves tree) returns the list of all leaves of the binary tree tree

lisp

(defun leaves (tree)
  (cond
   ((null tree) ())
   ((and (null (second tree)) (null (third tree))) (list tree))
   (t (append (leaves (second tree)) (leaves (third tree))))
   )
  )
Be first to comment
Leave a reply