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 { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } let sum ; function getTotalHeightUtil ( root ) { if ( root == null ) { return 0 ; } let lh = getTotalHeightUtil ( root . left ) ; let rh = getTotalHeightUtil ( root . right ) ; let h = Math . max ( lh , rh ) + 1 ; sum = sum + h ; return h ; } function getTotalHeight ( root ) { sum = 0 ; getTotalHeightUtil ( root ) ; return sum ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( " " , getTotalHeight ( root ) ) ;