Sum of heights of all individual nodes in a binary tree | C # program to find sum of heights of all nodes in a binary tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new Node with the given data and null left and right pointers . ; Function to sum of heights of individual Nodes Uses Inorder traversal ; Driver code
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left ; public Node right ; } ; static int sum ; static Node newNode ( int data ) { Node Node = new Node ( ) ; Node . data = data ; Node . left = null ; Node . right = null ; return ( Node ) ; } static int getTotalHeightUtil ( Node root ) { if ( root == null ) { return 0 ; } int lh = getTotalHeightUtil ( root . left ) ; int rh = getTotalHeightUtil ( root . right ) ; int h = Math . Max ( lh , rh ) + 1 ; sum = sum + h ; return h ; } static int getTotalHeight ( Node root ) { sum = 0 ; getTotalHeightUtil ( root ) ; return sum ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . Write ( " Sum ▁ of ▁ heights ▁ of ▁ all ▁ Nodes ▁ = ▁ { 0 } " , getTotalHeight ( root ) ) ; } }