August 26, 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 methodinternalListto collect them in a list.

> p62:internalList( #node{value = "a", leftNode = #node{value = "b"}, rightNode = #node{ value = "c" , leftNode = #node{value = "d"}}}).
    ["a","c"]

erlang

% Count the leaves of a binary tree

module (p62).
-include ("node.hrl").
-export ([internalList / 1]).

internalList (#node { value = V, leftNode = L, rightNode = R }) - >
    case [L, R] of
        [null, null] - > [];
        [null, RR] - > [V] ++ internalList (RR);
        [LL, null] - > [V] ++ internalList (LL);
        [LL, RR] - > [V] ++ internalList (LL) ++ internalList (RR)
    than.
Be first to comment
Leave a reply