LeetCode 3067: Count Pairs of Connectable Servers in a Weighted Tree Network Solution

Master LeetCode problem 3067 (Count Pairs of Connectable Servers in a Weighted Tree Network), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3067. Count Pairs of Connectable Servers in a Weighted Tree Network

Problem Explanation

Explanation

To solve this problem, we can use a depth-first search (DFS) traversal on the tree to calculate the number of nodes on the left and right side of each node. We can then multiply these counts to get the total number of pairs connectable through that node. The main idea is to maintain two arrays, leftCount and rightCount, to store the number of nodes on the left and right side of each node, respectively.

  1. Perform a DFS traversal to calculate the total number of nodes in the subtree rooted at each node.
  2. For each node i, calculate the number of nodes on its left side as leftCount[i] = totalNodes - rightCount[i] - 1.
  3. Calculate the total number of pairs connectable through each node as pairsCount[i] = leftCount[i] * rightCount[i].

Time complexity: O(N), where N is the number of nodes in the tree. Space complexity: O(N) for storing the leftCount, rightCount, and pairsCount arrays.

Solution Code

class Solution {
    public int[] countPairs(int n, int[][] edges, int signalSpeed) {
        List<List<Integer>> adjList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            adjList.add(new ArrayList<>());
        }
        for (int[] edge : edges) {
            adjList.get(edge[0]).add(edge[1]);
            adjList.get(edge[1]).add(edge[0]);
        }
        
        int[] leftCount = new int[n];
        int[] rightCount = new int[n];
        
        dfs(0, -1, adjList, leftCount, rightCount);
        
        int[] pairsCount = new int[n];
        for (int i = 0; i < n; i++) {
            pairsCount[i] = leftCount[i] * rightCount[i];
        }
        
        return pairsCount;
    }
    
    private void dfs(int node, int parent, List<List<Integer>> adjList, int[] leftCount, int[] rightCount) {
        int totalNodes = 1;
        for (int neighbor : adjList.get(node)) {
            if (neighbor == parent) continue;
            dfs(neighbor, node, adjList, leftCount, rightCount);
            totalNodes += leftCount[neighbor] + rightCount[neighbor];
            leftCount[node] += rightCount[neighbor];
            rightCount[node] += leftCount[neighbor];
        }
        leftCount[node] = totalNodes - rightCount[node] - 1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3067 (Count Pairs of Connectable Servers in a Weighted Tree Network)?

This page provides optimized solutions for LeetCode problem 3067 (Count Pairs of Connectable Servers in a Weighted Tree Network) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3067 (Count Pairs of Connectable Servers in a Weighted Tree Network)?

The time complexity for LeetCode 3067 (Count Pairs of Connectable Servers in a Weighted Tree Network) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3067 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3067 in Java, C++, or Python.

Back to LeetCode Solutions