finding the maximum product of a path in a binary tree

The product of a path can only be known when all its nodes have been visited and no partial path is decisive. So you have to traverse the entire tree. Arrange a standard tree traversal, but keep a trace of the products along the partial paths, and on every node update the maximum so far.

(Your solution abandons some paths prematurely.)

A simple solution:

if (is_leaf(t))
    return t->value;
    return max(t->value * product(t->left), t->value * product(t->right));

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top