Python program to check for children sum property in a binary tree














































Python program to check for children sum property in a binary tree



DESCRIPTION:
The program to check the children sum property in a binary tree is to returns true, if for every node, the value of the node is equal to the sum of data values in the left and the right children.


                     


PROGRAM:
 

class Node: def __init__(self, data): self.data = data self.left = self.right = None def sum_of_children_property(node): left_data = 0 right_data = 0 if(node == None or (node.left == None and node.right == None)): return 1 else: if(node.left != None): left_data = node.left.data if(node.right != None): right_data = node.right.data if((node.data == left_data + right_data) and sum_of_children_property(node.left) and sum_of_children_property(node.right)): return 1 else: return 0 if __name__ == '__main__': root = Node(13) root.left = Node(5) root.right = Node(8) root.left.left = Node(2) root.left.right = Node(3) root.right.right = Node(6) root.right.left = Node(2) if(sum_of_children_property(root)): print("The children sum property is satisfied") else: print("The children sum property does not satisfy")


OUTPUT:
The children sum property is satisfied




Comments