Sum of lengths of all paths possible in a given tree
Last Updated :
12 Jul, 2025
Given a tree with N nodes, the task is to find the sum of lengths of all the paths. Path length for two nodes in the tree is the number of edges on the path and for two adjacent nodes in the tree, the length of the path is 1.
Examples:
Input:
0
/ \
1 2
/ \
3 4
Output: 18
Paths of length 1 = (0, 1), (0, 2), (1, 3), (1, 4) = 4
Paths of length 2 = (0, 3), (0, 4), (1, 2), (3, 4) = 4
Paths of length 3 = (3, 2), (4, 2) = 2
The sum of lengths of all paths =
(4 * 1) + (4 * 2) + (2 * 3) = 18
Input:
0
/
1
/
2
Output: 4
Naive approach: Check all possible paths and then add them to compute the final result. The complexity of this approach will be O(n2).
Efficient approach: It can be noted that each edge in a tree is a bridge. Hence that edge is going to be present in every path possible between the two subtrees that the edge connects.
For example, the edge (1 - 0) is present in every path possible between {1, 3, 4} and {0, 2}, (1 - 0) is used for 6 times that is size of the subtree {1, 3, 4} multiplied by the size of the subtree {0, 2}. So for each edge, compute how many times that edge is going to be considered for the paths going over it. DFS can be used to store the size of the subtree and the contribution of all edges can be computed with another dfs. The complexity of this approach will be O(n).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5;
// Number of vertices
int n;
// Adjacency list representation
// of the tree
vector<int> tree[sz];
// Array that stores the subtree size
int subtree_size[sz];
// Array to mark all the
// vertices which are visited
int vis[sz];
// Utility function to create an
// edge between two vertices
void addEdge(int a, int b)
{
// Add a to b's list
tree[a].push_back(b);
// Add b to a's list
tree[b].push_back(a);
}
// Function to calculate the subtree size
int dfs(int node)
{
// Mark visited
vis[node] = 1;
subtree_size[node] = 1;
// For every adjacent node
for (auto child : tree[node]) {
// If not already visited
if (!vis[child]) {
// Recursive call for the child
subtree_size[node] += dfs(child);
}
}
return subtree_size[node];
}
// Function to calculate the
// contribution of each edge
void contribution(int node, int& ans)
{
// Mark current node as visited
vis[node] = true;
// For every adjacent node
for (int child : tree[node]) {
// If it is not already visited
if (!vis[child]) {
ans += (subtree_size[child]
* (n - subtree_size[child]));
contribution(child, ans);
}
}
}
// Function to return the required sum
int getSum()
{
// First pass of the dfs
memset(vis, 0, sizeof(vis));
dfs(0);
// Second pass
int ans = 0;
memset(vis, 0, sizeof(vis));
contribution(0, ans);
return ans;
}
// Driver code
int main()
{
n = 5;
addEdge(0, 1);
addEdge(0, 2);
addEdge(1, 3);
addEdge(1, 4);
cout << getSum();
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
@SuppressWarnings("unchecked")
class GFG{
static int sz = 100005;
// Number of vertices
static int n;
// Adjacency list representation
// of the tree
static ArrayList []tree = new ArrayList[sz];
// Array that stores the subtree size
static int []subtree_size = new int[sz];
// Array to mark all the
// vertices which are visited
static int []vis = new int[sz];
// Utility function to create an
// edge between two vertices
static void AddEdge(int a, int b)
{
// Add a to b's list
tree[a].add(b);
// Add b to a's list
tree[b].add(a);
}
// Function to calculate the subtree size
static int dfs(int node)
{
// Mark visited
vis[node] = 1;
subtree_size[node] = 1;
// For every adjacent node
for(int child : (ArrayList<Integer>)tree[node])
{
// If not already visited
if (vis[child] == 0)
{
// Recursive call for the child
subtree_size[node] += dfs(child);
}
}
return subtree_size[node];
}
// Function to calculate the
// contribution of each edge
static int contribution(int node, int ans)
{
// Mark current node as visited
vis[node] = 1;
// For every adjacent node
for(int child : (ArrayList<Integer>)tree[node])
{
// If it is not already visited
if (vis[child] == 0)
{
ans += (subtree_size[child] *
(n - subtree_size[child]));
ans = contribution(child, ans);
}
}
return ans;
}
// Function to return the required sum
static int getSum()
{
// First pass of the dfs
Arrays.fill(vis, 0);
dfs(0);
// Second pass
int ans = 0;
Arrays.fill(vis, 0);
ans = contribution(0, ans);
return ans;
}
// Driver code
public static void main(String []args)
{
n = 5;
for(int i = 0; i < sz; i++)
{
tree[i] = new ArrayList();
}
AddEdge(0, 1);
AddEdge(0, 2);
AddEdge(1, 3);
AddEdge(1, 4);
System.out.println(getSum());
}
}
// This code is contributed by pratham76
Python3
# Python3 implementation of the approach
sz = 10**5
# Number of vertices
n = 5
an = 0
# Adjacency list representation
# of the tree
tree = [[] for i in range(sz)]
# Array that stores the subtree size
subtree_size = [0] * sz
# Array to mark all the
# vertices which are visited
vis = [0] * sz
# Utility function to create an
# edge between two vertices
def addEdge(a, b):
# Add a to b's list
tree[a].append(b)
# Add b to a's list
tree[b].append(a)
# Function to calculate the subtree size
def dfs(node):
leaf = True
# Mark visited
vis[node] = 1
# For every adjacent node
for child in tree[node]:
# If not already visited
if (vis[child] == 0):
leaf = False
dfs(child)
# Recursive call for the child
subtree_size[node] += subtree_size[child]
if leaf:
subtree_size[node] = 1
# Function to calculate the
# contribution of each edge
def contribution(node,ans):
global an
# Mark current node as visited
vis[node] = 1
# For every adjacent node
for child in tree[node]:
# If it is not already visited
if (vis[child] == 0):
an += (subtree_size[child] *
(n - subtree_size[child]))
contribution(child, ans)
# Function to return the required sum
def getSum():
# First pass of the dfs
for i in range(sz):
vis[i] = 0
dfs(0)
# Second pass
ans = 0
for i in range(sz):
vis[i] = 0
contribution(0, ans)
return an
# Driver code
n = 5
addEdge(0, 1)
addEdge(0, 2)
addEdge(1, 3)
addEdge(1, 4)
print(getSum())
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static int sz = 100005;
// Number of vertices
static int n;
// Adjacency list representation
// of the tree
static ArrayList []tree = new ArrayList[sz];
// Array that stores the subtree size
static int []subtree_size = new int[sz];
// Array to mark all the
// vertices which are visited
static int []vis = new int[sz];
// Utility function to create an
// edge between two vertices
static void addEdge(int a, int b)
{
// Add a to b's list
tree[a].Add(b);
// Add b to a's list
tree[b].Add(a);
}
// Function to calculate the subtree size
static int dfs(int node)
{
// Mark visited
vis[node] = 1;
subtree_size[node] = 1;
// For every adjacent node
foreach(int child in tree[node])
{
// If not already visited
if (vis[child] == 0)
{
// Recursive call for the child
subtree_size[node] += dfs(child);
}
}
return subtree_size[node];
}
// Function to calculate the
// contribution of each edge
static void contribution(int node, ref int ans)
{
// Mark current node as visited
vis[node] = 1;
// For every adjacent node
foreach(int child in tree[node])
{
// If it is not already visited
if (vis[child] == 0)
{
ans += (subtree_size[child] *
(n - subtree_size[child]));
contribution(child, ref ans);
}
}
}
// Function to return the required sum
static int getSum()
{
// First pass of the dfs
Array.Fill(vis, 0);
dfs(0);
// Second pass
int ans = 0;
Array.Fill(vis, 0);
contribution(0, ref ans);
return ans;
}
// Driver code
public static void Main()
{
n = 5;
for(int i = 0; i < sz; i++)
{
tree[i] = new ArrayList();
}
addEdge(0, 1);
addEdge(0, 2);
addEdge(1, 3);
addEdge(1, 4);
Console.Write(getSum());
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript implementation of the approach
let sz = 100005;
// Number of vertices
let n;
// Adjacency list representation
// of the tree
let tree = new Array(sz);
// Array that stores the subtree size
let subtree_size = new Array(sz);
// Array to mark all the
// vertices which are visited
let vis = new Array(sz);
// Utility function to create an
// edge between two vertices
function AddEdge(a,b)
{
// Add a to b's list
tree[a].push(b);
// Add b to a's list
tree[b].push(a);
}
// Function to calculate the subtree size
function dfs(node)
{
// Mark visited
vis[node] = 1;
subtree_size[node] = 1;
// For every adjacent node
for(let child=0;child<tree[node].length;child++)
{
// If not already visited
if (vis[tree[node][child]] == 0)
{
// Recursive call for the child
subtree_size[node] += dfs(tree[node][child]);
}
}
return subtree_size[node];
}
// Function to calculate the
// contribution of each edge
function contribution(node,ans)
{
// Mark current node as visited
vis[node] = 1;
// For every adjacent node
for(let child=0;child<tree[node].length;child++)
{
// If it is not already visited
if (vis[tree[node][child]] == 0)
{
ans += (subtree_size[tree[node][child]] *
(n - subtree_size[tree[node][child]]));
ans = contribution(tree[node][child], ans);
}
}
return ans;
}
// Function to return the required sum
function getSum()
{
// First pass of the dfs
for(let i=0;i<vis.length;i++)
{
vis[i]=0;
}
dfs(0);
// Second pass
let ans = 0;
for(let i=0;i<vis.length;i++)
{
vis[i]=0;
}
ans = contribution(0, ans);
return ans;
}
// Driver code
n = 5;
for(let i = 0; i < sz; i++)
{
tree[i] = [];
}
AddEdge(0, 1);
AddEdge(0, 2);
AddEdge(1, 3);
AddEdge(1, 4);
document.write(getSum());
// This code is contributed by patel2127
</script>
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem