human
What is a code to sum of heights of all individual nodes in a binary tree and the other steps required given the following:
Sum of heights of all individual nodes in a binary tree | Compute the " maxHeight " of a particular Node.
compute the height of each subtree.
use the larger one.
Helper class that allocates a new Node with the given data and None left and right pointers ..
Function to sum of heights of individual Nodes Uses Inorder traversal.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to sum of heights of all individual nodes in a binary tree and the other steps required.
 def getHeight ( Node ) :
	if ( Node == None ) :
		return 0
	else :
		lHeight = getHeight ( Node . left )
		rHeight = getHeight ( Node . right )
		if ( lHeight > rHeight ) :
			return ( lHeight + 1 )
		else :
			return ( rHeight + 1 )
class newNode :
	def __init__ ( self , data ) :
		self . data = data
		self . left = None
		self . right = None
def getTotalHeight ( root ) :
	if ( root == None ) :
		return 0
	return ( getTotalHeight ( root . left ) + getHeight ( root ) + getTotalHeight ( root . right ) )
if __name__ == ' _ _ main _ _ ' :
	root = newNode ( 1 )
	root . left = newNode ( 2 )
	root . right = newNode ( 3 )
	root . left . left = newNode ( 4 )
	root . left . right = newNode ( 5 )
print ( " Sum ▁ of ▁ heights ▁ of ▁ all ▁ Nodes ▁ = " , getTotalHeight ( root ) )