Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

834. Sum of Distances in Tree #27

Open
F4NT0 opened this issue Apr 29, 2024 · 1 comment
Open

834. Sum of Distances in Tree #27

F4NT0 opened this issue Apr 29, 2024 · 1 comment
Assignees
Labels
Hard Hard Questions Ready to code Description from the Question was updated with code and text

Comments

@F4NT0
Copy link
Owner

F4NT0 commented Apr 29, 2024

There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.

You are given the integer n and the array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.

Return an array answer of length n where answer[i] is the sum of the distances between the ith node in the tree and all other nodes.

Example 1

image

  • Input: n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
  • Output: [8,12,6,10,10,10]
  • Explanation: The tree is shown above.
  • We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
  • equals 1 + 1 + 2 + 2 + 2 = 8.
  • Hence, answer[0] = 8, and so on.

Example 2

image

  • Input: n = 1, edges = []
  • Output: [0]

Example 3

image

  • Input: n = 2, edges = [[1,0]]
  • Output: [1,1]

Constraints:

  • 1 <= n <= 3 * 104
  • edges.length == n - 1
  • edges[i].length == 2
  • 0 <= ai, bi < n
  • ai != bi
  • The given input represents a valid tree.
@F4NT0 F4NT0 added the Hard Hard Questions label Apr 29, 2024
@F4NT0 F4NT0 added this to the April Leetcode milestone Apr 29, 2024
@F4NT0 F4NT0 self-assigned this Apr 29, 2024
@F4NT0
Copy link
Owner Author

F4NT0 commented May 5, 2024

using System;
using System.Collections.Generic;

public class Solution {
    public int[] SumOfDistancesInTree(int n, int[][] edges) {
        int[] ans = new int[n];
        int[] count = new int[n];
        HashSet<int>[] tree = new HashSet<int>[n];

        for (int i = 0; i < n; ++i) {
            tree[i] = new HashSet<int>();
            count[i] = 1;
        }

        foreach (int[] edge in edges) {
            int u = edge[0];
            int v = edge[1];
            tree[u].Add(v);
            tree[v].Add(u);
        }

        Postorder(tree, 0, -1, count, ans);
        Preorder(tree, 0, -1, count, ans);
        return ans;
    }

    private void Postorder(HashSet<int>[] tree, int node, int parent, int[] count, int[] ans) {
        foreach (int child in tree[node]) {
            if (child == parent)
                continue;
            Postorder(tree, child, node, count, ans);
            count[node] += count[child];
            ans[node] += ans[child] + count[child];
        }
    }

    private void Preorder(HashSet<int>[] tree, int node, int parent, int[] count, int[] ans) {
        foreach (int child in tree[node]) {
            if (child == parent)
                continue;
            ans[child] = ans[node] - count[child] + (tree.Length - count[child]);
            Preorder(tree, child, node, count, ans);
        }
    }
}

@F4NT0 F4NT0 added the Ready to code Description from the Question was updated with code and text label May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hard Hard Questions Ready to code Description from the Question was updated with code and text
Projects
None yet
Development

No branches or pull requests

1 participant