Difference between sums of odd level and even level nodes of a Binary Tree | A Binary Tree node ; The main function that returns difference between odd and even level nodes ; Base Case ; Difference for root is root 's data - difference for  left subtree - difference for right subtree ; Driver program to test above function
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 getLevelDiff ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( root . data - getLevelDiff ( root . left ) - getLevelDiff ( root . right ) ) NEW_LINE DEDENT root = Node ( 5 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 6 ) NEW_LINE root . left . left = Node ( 1 ) NEW_LINE root . left . right = Node ( 4 ) NEW_LINE root . left . right . left = Node ( 3 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE root . right . right . right = Node ( 9 ) NEW_LINE root . right . right . left = Node ( 7 ) NEW_LINE print " % d ▁ is ▁ the ▁ required ▁ difference " % ( getLevelDiff ( root ) ) NEW_LINE