Sum of heights of all individual nodes in a binary tree | A binary tree Node has data , pointer to left child and a pointer to right child ; Function to sum of heights of individual Nodes Uses Inorder traversal ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT sum = 0 NEW_LINE def getTotalHeightUtil ( root ) : NEW_LINE INDENT global sum NEW_LINE if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT lh = getTotalHeightUtil ( root . left ) NEW_LINE rh = getTotalHeightUtil ( root . right ) NEW_LINE h = max ( lh , rh ) + 1 NEW_LINE sum = sum + h NEW_LINE return h NEW_LINE DEDENT def getTotalHeight ( root ) : NEW_LINE INDENT getTotalHeightUtil ( root ) NEW_LINE 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 print ( " Sum ▁ of ▁ heights ▁ of ▁ all ▁ Nodes ▁ = " , getTotalHeight ( root ) ) NEW_LINE DEDENT