Given a Binary Tree, find the sum of all the leaf nodes that are at minimum level of the given binary tree.
Examples:
Input:
Output: 13 Explanation: The leaf nodes of the tree are 5, 7, 2 and 8. Among these leaf nodes, the minimum level leaf nodes are 5 and 8. So, the required sum = 5 + 8 = 13.
Input:
Output: 22 Explanation: The leaf nodes of the tree are 4, 5, 6 and 7. All the leaf nodes are present at the same minimum level. So, the required sum = 4 + 5 + 6 + 7 = 22.
The idea is to perform a level order traversal of the binary tree using a queue. During traversal, we process nodes level by level and find the first level that contains a leaf node. Once this level is found, we sum all the leaf nodes present at this level and stop further traversal because this is the minimum level containing leaf nodes.
Let us understand with an example:
Start BFS traversal from root node 1. Queue becomes: [1]
Process level 0. Node 1 is not a leaf node, push its children 2 and 3 into the queue. Queue becomes: [2, 3]
Process level 1. Nodes 2 and 3 are not leaf nodes, so push their children 4, 5, 6 and 7. Queue becomes: [4, 5, 6, 7]
Process level 2. Nodes 4, 5, 6 and 7 are leaf nodes, so add them to sum. sum = 4 + 5 + 6 + 7 = 22
Since the first level containing leaf nodes is found, stop traversal and return: 22
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intval){data=val;left=right=nullptr;}};// Function to find the sum of// leaf nodes at minimum levelintminLeafSum(Node*root){// If tree is emptyif(root==nullptr)return0;// If root itself is a leaf nodeif(root->left==nullptr&&root->right==nullptr)returnroot->data;// Queue for level order traversalqueue<Node*>q;q.push(root);intsum=0;boolfound=false;while(!q.empty()&&!found){// Number of nodes at current levelintsize=q.size();while(size--){Node*curr=q.front();q.pop();// Check if current node is a leaf nodeif(curr->left==nullptr&&curr->right==nullptr){sum+=curr->data;found=true;}else{// Push left childif(curr->left)q.push(curr->left);// Push right childif(curr->right)q.push(curr->right);}}}returnsum;}// Driver codeintmain(){Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);root->right->left=newNode(6);root->right->right=newNode(7);cout<<minLeafSum(root);return0;}
Java
importjava.util.Queue;importjava.util.LinkedList;classNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intval){data=val;left=right=null;}}publicclassGfG{// Function to find the sum of// leaf nodes at minimum levelpublicstaticintminLeafSum(Noderoot){// If tree is emptyif(root==null)return0;// If root itself is a leaf nodeif(root.left==null&&root.right==null)returnroot.data;// Queue for level order traversalQueue<Node>q=newLinkedList<>();q.add(root);intsum=0;booleanfound=false;while(!q.isEmpty()&&!found){// Number of nodes at current levelintsize=q.size();while(size-->0){Nodecurr=q.poll();// Check if current node is a leaf nodeif(curr.left==null&&curr.right==null){sum+=curr.data;found=true;}else{// Push left childif(curr.left!=null)q.add(curr.left);// Push right childif(curr.right!=null)q.add(curr.right);}}}returnsum;}publicstaticvoidmain(String[]args){Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);root.right.right=newNode(7);System.out.println(minLeafSum(root));}}
Python
fromcollectionsimportdequeclassNode:def__init__(self,val):self.data=valself.left=Noneself.right=None# Function to find the sum of# leaf nodes at minimum leveldefminLeafSum(root):# If tree is emptyifrootisNone:return0# If root itself is a leaf nodeifroot.leftisNoneandroot.rightisNone:returnroot.data# Queue for level order traversalq=deque([root])sum=0found=Falsewhileqandnotfound:# Number of nodes at current levelsize=len(q)whilesize>0:curr=q.popleft()# Check if current node is a leaf nodeifcurr.leftisNoneandcurr.rightisNone:sum+=curr.datafound=Trueelse:# Push left childifcurr.leftisnotNone:q.append(curr.left)# Push right childifcurr.rightisnotNone:q.append(curr.right)size-=1returnsum# Driver codeif__name__=='__main__':root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(4)root.left.right=Node(5)root.right.left=Node(6)root.right.right=Node(7)print(minLeafSum(root))
C#
usingSystem;usingSystem.Collections.Generic;publicclassNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intval){data=val;left=right=null;}}publicclassGfG{// Function to find the sum of// leaf nodes at minimum levelpublicstaticintminLeafSum(Noderoot){// If tree is emptyif(root==null)return0;// If root itself is a leaf nodeif(root.left==null&&root.right==null)returnroot.data;// Queue for level order traversalQueue<Node>q=newQueue<Node>();q.Enqueue(root);intsum=0;boolfound=false;while(q.Count>0&&!found){// Number of nodes at current levelintsize=q.Count;while(size-->0){Nodecurr=q.Dequeue();// Check if current node is a leaf nodeif(curr.left==null&&curr.right==null){sum+=curr.data;found=true;}else{// Push left childif(curr.left!=null)q.Enqueue(curr.left);// Push right childif(curr.right!=null)q.Enqueue(curr.right);}}}returnsum;}publicstaticvoidMain(){Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);root.right.right=newNode(7);Console.WriteLine(minLeafSum(root));}}
JavaScript
classNode{constructor(val){this.data=val;this.left=null;this.right=null;}}// Function to find the sum of// leaf nodes at minimum levelfunctionminLeafSum(root){// If tree is emptyif(root===null)return0;// If root itself is a leaf nodeif(root.left===null&&root.right===null)returnroot.data;// Queue for level order traversalletq=[root];letsum=0;letfound=false;while(q.length>0&&!found){// Number of nodes at current levelletsize=q.length;while(size-->0){letcurr=q.shift();// Check if current node is a leaf nodeif(curr.left===null&&curr.right===null){sum+=curr.data;found=true;}else{// Push left childif(curr.left!==null)q.push(curr.left);// Push right childif(curr.right!==null)q.push(curr.right);}}}returnsum;}// Driver codeletroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);root.right.right=newNode(7);console.log(minLeafSum(root));