< Summary

Information
Class: LeetCode.Algorithms.SumOfDistancesInTree.SumOfDistancesInTreeDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumOfDistancesInTree\SumOfDistancesInTreeDepthFirstSearch.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SumOfDistancesInTree(...)100%44100%
Dfs(...)100%44100%
Dfs2(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumOfDistancesInTree\SumOfDistancesInTreeDepthFirstSearch.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.SumOfDistancesInTree;
 13
 14/// <inheritdoc />
 15public class SumOfDistancesInTreeDepthFirstSearch : ISumOfDistancesInTree
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <param name="edges"></param>
 23    /// <returns></returns>
 24    public int[] SumOfDistancesInTree(int n, int[][] edges)
 325    {
 326        var graph = new List<int>[n];
 27
 2428        for (var i = 0; i < n; i++)
 929        {
 930            graph[i] = [];
 931        }
 32
 2133        foreach (var edge in edges)
 634        {
 1235            int u = edge[0], v = edge[1];
 36
 637            graph[u].Add(v);
 638            graph[v].Add(u);
 639        }
 40
 341        var count = new int[n];
 342        var ans = new int[n];
 43
 344        Dfs(0, -1, graph, count, ans);
 45
 346        Dfs2(0, -1, graph, count, ans);
 47
 348        return ans;
 349    }
 50
 51    private static void Dfs(int node, int parent, List<int>[] graph, int[] count, int[] ans)
 952    {
 953        count[node] = 1;
 54
 5155        foreach (var child in graph[node].Where(child => child != parent))
 656        {
 657            Dfs(child, node, graph, count, ans);
 58
 659            count[node] += count[child];
 660            ans[node] += ans[child] + count[child];
 661        }
 962    }
 63
 64    private static void Dfs2(int node, int parent, List<int>[] graph, int[] count, int[] ans)
 965    {
 5166        foreach (var child in graph[node].Where(child => child != parent))
 667        {
 668            ans[child] = ans[node] - count[child] + count.Length - count[child];
 69
 670            Dfs2(child, node, graph, count, ans);
 671        }
 972    }
 73}