Thus, the computation for the leaves are identical to
that in the binary tree.
If , then
We now consider non-leaf nodes . Let
be the number of children of
, let
be its children from left to right, and let the subtrees
rooted at the children of
be
respectively.
Denote by
the subtree of
induced by the vertex set of
,
for
.
Denote by
the total number of leaves in
.
Define
, where
or
,
,
, and
, as follows.
First let
.
If
,
is the
minimal cost of a pebbling placement of the subtree
,
where we use at most
pebbles in
,
and no pebble is placed on
.
(When no feasible pebbling placement exists with this constraint
we have
.)
If
,
is the
same as above except
is placed with a pebble out of
pebbles.
This definition is generalized for .
For
,
we consider
in place of
and
for
we stipulate that one additional pebble is
placed at the tip of the external path from
of distance
from
. As before this additional pebble is
not counted in
.
We then define
Again we will compute for all
,
inductively for
according to the height of
the subtree
, starting with leaves
.
The base case
having already been taken care of, we assume
and
.
First we consider the left most subtree with
,
i.e., we compute
for
.
If and
, then
Similarly for and
, then
Now we consider . Again if
,
Similarly for and
,
We proceed to the case of
. This time we inductively assume
that we have already computed
not only all
with
, but also the relevant quantities
for
.
Thus, for and
,
For and
,
Finally we consider the case and
.
For
and
, we have
And for and
, we have
We have completed the description of the algorithm.
The final answer is ,
where
is the root of
and
is the number of pebbles.
Again, no need to compute for any value
,
if
is the total number of pebbles given.
The complexity of the algorithm can be easily
estimated as before.
For leaves, the algorithm spends time per leaf.
Thus the total work spent on leaves is at most
.
For any non-leaf
, suppose the degree of
is
,
then the computation work spent for
is
.
Thus the total amount of work spent for non-leaves is
.
Hence the total running time is at most
,
which is again only
with
.