Check sum of Covered and Uncovered nodes of Binary Tree | To create a newNode of tree and return pointer ; Utility function to calculate sum of all node of tree ; Recursive function to calculate sum of left boundary elements ; If leaf node , then just return its key value ; If left is available then go left otherwise go right ; Recursive function to calculate sum of right boundary elements ; If leaf node , then just return its key value ; If right is available then go right otherwise go left ; Returns sum of uncovered elements ; Initializing with 0 in case we don 't have left or right boundary  ; returning sum of root node , left boundary and right boundary ; Returns true if sum of covered and uncovered elements is same . ; Sum of uncovered elements ; Sum of all elements ; Check if sum of covered and uncovered is same ; Helper function to prinorder traversal of binary tree ; Driver Code ; Making above given diagram 's   binary tree
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def Sum ( t ) : NEW_LINE INDENT if ( t == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return t . key + Sum ( t . left ) + Sum ( t . right ) NEW_LINE DEDENT def uncoveredSumLeft ( t ) : NEW_LINE INDENT if ( t . left == None and t . right == None ) : NEW_LINE INDENT return t . key NEW_LINE DEDENT if ( t . left != None ) : NEW_LINE INDENT return t . key + uncoveredSumLeft ( t . left ) NEW_LINE DEDENT else : NEW_LINE INDENT return t . key + uncoveredSumLeft ( t . right ) NEW_LINE DEDENT DEDENT def uncoveredSumRight ( t ) : NEW_LINE INDENT if ( t . left == None and t . right == None ) : NEW_LINE INDENT return t . key NEW_LINE DEDENT if ( t . right != None ) : NEW_LINE INDENT return t . key + uncoveredSumRight ( t . right ) NEW_LINE DEDENT else : NEW_LINE INDENT return t . key + uncoveredSumRight ( t . left ) NEW_LINE DEDENT DEDENT def uncoverSum ( t ) : NEW_LINE INDENT lb = 0 NEW_LINE rb = 0 NEW_LINE if ( t . left != None ) : NEW_LINE INDENT lb = uncoveredSumLeft ( t . left ) NEW_LINE DEDENT if ( t . right != None ) : NEW_LINE INDENT rb = uncoveredSumRight ( t . right ) NEW_LINE DEDENT return t . key + lb + rb NEW_LINE DEDENT def isSumSame ( root ) : NEW_LINE INDENT sumUC = uncoverSum ( root ) NEW_LINE sumT = Sum ( root ) NEW_LINE return ( sumUC == ( sumT - sumUC ) ) NEW_LINE DEDENT def inorder ( root ) : NEW_LINE INDENT if ( root ) : NEW_LINE INDENT inorder ( root . left ) NEW_LINE print ( root . key , end = " ▁ " ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 8 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 4 ) NEW_LINE root . left . right . right = newNode ( 7 ) NEW_LINE root . right = newNode ( 10 ) NEW_LINE root . right . right = newNode ( 14 ) NEW_LINE root . right . right . left = newNode ( 13 ) NEW_LINE if ( isSumSame ( root ) ) : NEW_LINE INDENT print ( " Sum ▁ of ▁ covered ▁ and ▁ uncovered ▁ is ▁ same " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Sum ▁ of ▁ covered ▁ and ▁ uncovered ▁ is ▁ not ▁ same " ) NEW_LINE DEDENT DEDENT