Check for Children Sum Property in a Binary Tree | returns 1 if children sum property holds for the given node and both of its children ; left_data is left child data and right_data is for right child data ; If node is None or it 's a leaf   node then return true  ; If left child is not present then 0 is used as data of left child ; If right child is not present then 0 is used as data of right child ; if the node and both of its children satisfy the property return 1 else 0 ; Helper class that allocates a new node with the given data and None left and right pointers . ; Driver Code
def isSumProperty ( node ) : NEW_LINE INDENT left_data = 0 NEW_LINE right_data = 0 NEW_LINE if ( node == None or ( node . left == None and node . right == None ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( node . left != None ) : NEW_LINE INDENT left_data = node . left . data NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT right_data = node . right . data NEW_LINE DEDENT if ( ( node . data == left_data + right_data ) and isSumProperty ( node . left ) and isSumProperty ( node . right ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT class newNode : 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 if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 8 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 2 ) NEW_LINE if ( isSumProperty ( root ) ) : NEW_LINE INDENT print ( " The ▁ given ▁ tree ▁ satisfies ▁ the " , " children ▁ sum ▁ property ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ given ▁ tree ▁ does ▁ not ▁ satisfy " , " the ▁ children ▁ sum ▁ property ▁ " ) NEW_LINE DEDENT DEDENT