Given a binary tree, find the sum of heights of all individual nodes in the tree.
Example:

For this tree:
1). Height of Node 1 - 3
2). Height of Node 2 - 2
3). Height of Node 3 - 1
4). Height of Node 4 - 1
5). Height of Node 5 - 1
Adding all of them = 8
Prerequisites:- Height of binary tree
Simple Solution: We get the height of all individual nodes by parsing the tree in any of the following methods, i.e. Inorder, postorder, preorder(I performed inorder tree traversal), and getting their heights using the getHeight function, which checks both left and right subtree and returns the maximum of them. Finally, we add up all the individual heights.
Implementation:
C++
#include <bits/stdc++.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int getHeight( struct Node* Node)
{
if (Node == NULL)
return 0;
else {
int lHeight = getHeight(Node->left);
int rHeight = getHeight(Node->right);
if (lHeight > rHeight)
return (lHeight + 1);
else
return (rHeight + 1);
}
}
struct Node* newNode( int data)
{
struct Node* Node = ( struct Node*)
malloc ( sizeof ( struct Node));
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
int getTotalHeight( struct Node* root)
{
if (root == NULL)
return 0;
return getTotalHeight(root->left) +
getHeight(root) +
getTotalHeight(root->right);
}
int main()
{
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf ( "Sum of heights of all Nodes = %d" ,
getTotalHeight(root));
return 0;
}
|
Java
class GfG {
static class Node {
int data;
Node left;
Node right;
}
static int getHeight(Node Node)
{
if (Node == null )
return 0 ;
else {
int lHeight = getHeight(Node.left);
int rHeight = getHeight(Node.right);
if (lHeight > rHeight)
return (lHeight + 1 );
else
return (rHeight + 1 );
}
}
static Node newNode( int data)
{
Node Node = new Node();
Node.data = data;
Node.left = null ;
Node.right = null ;
return (Node);
}
static int getTotalHeight( Node root)
{
if (root == null )
return 0 ;
return getTotalHeight(root.left) + getHeight(root) + getTotalHeight(root.right);
}
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 );
System.out.println( "Sum of heights of all Nodes = " + getTotalHeight(root));
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
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 )
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))
|
C#
using System;
class GfG
{
public class Node
{
public int data;
public Node left;
public Node right;
}
static int getHeight(Node Node)
{
if (Node == null )
return 0;
else
{
int lHeight = getHeight(Node.left);
int rHeight = getHeight(Node.right);
if (lHeight > rHeight)
return (lHeight + 1);
else
return (rHeight + 1);
}
}
static Node newNode( int data)
{
Node Node = new Node();
Node.data = data;
Node.left = null ;
Node.right = null ;
return (Node);
}
static int getTotalHeight( Node root)
{
if (root == null )
return 0;
return getTotalHeight(root.left) +
getHeight(root) +
getTotalHeight(root.right);
}
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 = " + getTotalHeight(root));
}
}
|
Javascript
<script>
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( "Sum of heights of all Nodes = " + getTotalHeight(root));
</script>
|
Output:
Sum of heights of all Nodes = 8
Time Complexity: O(nh) where n is the total number of nodes and h is the height of the binary tree.
Space complexity: O(n). This is because the recursive calls of getTotalHeight and getHeight functions require a stack size of O(n).
Efficient Solution: The idea is to compute heights and sum them up in the same recursive call.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode( int data)
{
struct Node* Node = ( struct Node*)
malloc ( sizeof ( struct Node));
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
int getTotalHeightUtil( struct Node* root, int &sum)
{
if (root == NULL)
return 0;
int lh = getTotalHeightUtil(root->left, sum);
int rh = getTotalHeightUtil(root->right, sum);
int h = max(lh, rh) + 1;
sum = sum + h;
return h;
}
int getTotalHeight(Node *root)
{
int sum = 0;
getTotalHeightUtil(root, sum);
return sum;
}
int main()
{
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf ( "Sum of heights of all Nodes = %d" ,
getTotalHeight(root));
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node left;
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 );
System.out.printf( "Sum of heights of all Nodes = %d" ,
getTotalHeight(root));
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
sum = 0
def getTotalHeightUtil(root):
global sum
if (root = = None ):
return 0
lh = getTotalHeightUtil(root.left)
rh = getTotalHeightUtil(root.right)
h = max (lh, rh) + 1
sum = sum + h
return h
def getTotalHeight(root):
getTotalHeightUtil(root)
return sum
if __name__ = = '__main__' :
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.left.right = Node( 5 )
print ( "Sum of heights of all Nodes =" ,
getTotalHeight(root))
|
C#
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));
}
}
|
Javascript
<script>
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( "Sum of heights of all Nodes = " ,
getTotalHeight(root));
</script>
|
Output:
Sum of heights of all Nodes = 8
Time Complexity: O(n) where n is the total number of nodes of the binary tree.
Space complexity: O(n), where n is the maximum depth of the binary tree. This space is used to store the recursive function calls on the stack.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!