Explore
ProGrad Programs
Placement Prep
TCS Codevita
Live Placement Training
Live Aptitude Training
Live Programming Training
Webinars
About Us

Edit
Reply




Edit

Find the product of all leaf nodes of a binary tree | FACE Prep

Published on 10 Mar 2020

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. The algorithm to solve this problem is as below.


  • Start traversing the tree from the root node.
  • Check if everynode 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 in a 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


C++

Output
Enter the umber of edges : 8
1 2 l
2 4 l
4 8 l
2 5 r
1 3 r
3 6 l
3 7 r
7 9 r
Product of the leaves : 2160


Time complexity: O(n)


Recommended Programs





If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'c plus plus'
Articles Practice Exercises