Sum of leaf nodes at minimum level | Python3 implementation to find the sum of leaf node at minimum level ; Structure of a node in binary tree ; function to find the sum of leaf nodes at minimum level ; if tree is empty ; if there is only root node ; Queue used for level order traversal ; push rioot node in the queue ; count no . of nodes present at current level ; traverse current level nodes ; get front element from ' q ' ; if node is leaf node ; accumulate data to ' sum ' ; set flag = 1 to signify that we have encountered the minimum level ; if top 's left or right child exist  push them to Queue ; return the sum ; Driver code ; binary tree creation
from collections import deque NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def sumOfLeafNodesAtLeafLevel ( root ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return 0 NEW_LINE DEDENT if not root . left and not root . right : NEW_LINE INDENT return root . data NEW_LINE DEDENT Queue = deque ( ) NEW_LINE sum = f = 0 NEW_LINE Queue . append ( root ) NEW_LINE while not f : NEW_LINE INDENT nc = len ( Queue ) NEW_LINE while nc : NEW_LINE INDENT top = Queue . popleft ( ) NEW_LINE if not top . left and not top . right : NEW_LINE INDENT sum += top . data NEW_LINE f = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if top . left : NEW_LINE INDENT Queue . append ( top . left ) NEW_LINE DEDENT if top . right : NEW_LINE INDENT Queue . append ( top . right ) NEW_LINE DEDENT DEDENT nc -= 1 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . left . right . left = Node ( 8 ) NEW_LINE root . right . left . right = Node ( 9 ) NEW_LINE print ( " Sum ▁ = ▁ " , sumOfLeafNodesAtLeafLevel ( root ) ) NEW_LINE DEDENT