'Multiplication of all nodes in a binary tree

I'm trying to multiply all nodes in a binary tree but I'm only getting 0 as a result. My code is:

product_tree(nil,0).
product_tree(t(L,X,R), N):-
   product_tree(L,L1),
   product_tree(R,R2),
   N is X * L1 * R2.


Solution 1:[1]

I think the problem is that in the base case, (when the tree is empty, in the first line), you are setting the result to 0. So when you get that base case, you are multiplying all your result by 0, which result is 0.

So, to fix that, try changing the 0 by 1 in the first line.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 José Carlos Gualo Cejudo