Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Get the Sum of all left leaves in a Binary tree

Objec­tive: Given a binary tree, find the sum of all the nodes which are left as well as leaves nodes.

Exam­ple:

 

Get the Sum of all left leaves in a Binary tree.

Get the Sum of all left leaves in a Binary tree.

 

Approach:

  1. Approach is quite simple.
  2. Do the inorder traversal
  3. check if node if the left child and leaf node.
  4. If yes then add it to the sum.
  5. See the code for more understanding.

Com­plete Code:

Out­put:

Sum of left leaves: 21

You may also like...

  • Aman Rustagi

    Tra­verse given binary tree using pre order tra­ver­sal. If cur­rent node is left leaf node, then add it’s value to vari­able sum of left node of binary tree. If cur­rent node is not a left leaf node then recur­sively cal­cu­late the sum of left leaf nodes of binary tree in left and right sub tree and return.