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 ; Compute the " maxHeight " of a particular Node ; compute the height of each subtree ; use the larger one ; 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 ; } } function getHeight ( Node ) { if ( Node == null ) return 0 ; else { let lHeight = getHeight ( Node . left ) ; let rHeight = getHeight ( Node . right ) ; if ( lHeight > rHeight ) return ( lHeight + 1 ) ; else return ( rHeight + 1 ) ; } } function getTotalHeight ( root ) { if ( root == null ) return 0 ; return getTotalHeight ( root . left ) + getHeight ( root ) + getTotalHeight ( root . right ) ; } 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 ) ) ;