August 19, 2024
P62 - Collect the internal nodes of a binary tree in a list
An internal node of a binary tree has either one or two non-empty successors. Write a function internals to collect them in a list.
(internals tree) returns the list of internal nodes of the binary tree tree.
lisp
(defun internals (tree)
(cond
((null tree) ())
((and (null (second tree)) (null (third tree))) ())
(t (cons tree (append (internals (second tree)) (internals (third tree)))))
)
)