Find the product of all leaf nodes of a binary tree | faceprep

Find the product of all leaf nodes of a binary tree | faceprep

Program to find the product of all leaf nodes of a binary tree is discussed here.

A leaf node is nothing but the terminal node, the node with no children.


Product of all leaf nodes of a binary tree




The algorithm to solve this problem is as below:

  • Start traversing the tree from the root node.
  • Check if every node is a leaf node or not.
  • If the node is a leaf node, then multiply the node with a variable temp_prod which is used to hold the product value of all the leaf nodes.




For example, consider the given tree


Product of all leaf nodes of a binary tree


  • The leaf nodes in the above binary tree are 8, 5, 6 and 9.
  • The product of the leaf nodes = 8*5*6*9 = 2160




Program to find the product of all leaf nodes of a binary tree

@@coding::1@@



Recommended Programs



c