LeetCode 3249: Count the Number of Good Nodes Solution

Master LeetCode problem 3249 (Count the Number of Good Nodes), 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.

3249. Count the Number of Good Nodes

Problem Explanation

Explanation

To solve this problem, we can perform a depth-first search (DFS) traversal of the tree starting from the root node. During the traversal, we can keep track of the size of each subtree rooted at a particular node. If all the sizes of the subtrees rooted at the children of a node are the same, then that node is considered a good node.

We can implement the DFS traversal using recursion. At each node, we calculate the sizes of the subtrees rooted at its children and check if they are all the same. If they are, we increment the count of good nodes.

Time Complexity:

  • The time complexity of this approach is O(n), where n is the number of nodes in the tree.

Space Complexity:

  • The space complexity of this approach is O(n) for the recursive call stack.

Solution Code

class Solution {
    int count = 0;
    
    public int countGoodNodes(int n, int[][] edges) {
        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]);
        }
        
        dfs(0, -1, adjList, new int[n]);
        
        return count;
    }
    
    private int[] dfs(int node, int parent, List<List<Integer>> adjList, int[] subtreeSizes) {
        int currentSize = 1;
        for (int child : adjList.get(node)) {
            if (child != parent) {
                int[] childSizes = dfs(child, node, adjList, subtreeSizes);
                currentSize += childSizes[0];
                subtreeSizes[child] = childSizes[0];
            }
        }
        
        boolean goodNode = true;
        for (int child : adjList.get(node)) {
            if (child != parent && subtreeSizes[child] != currentSize - subtreeSizes[child]) {
                goodNode = false;
                break;
            }
        }
        
        if (goodNode) {
            count++;
        }
        
        return new int[] {currentSize, 0};
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3249 (Count the Number of Good Nodes)?

This page provides optimized solutions for LeetCode problem 3249 (Count the Number of Good Nodes) 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 3249 (Count the Number of Good Nodes)?

The time complexity for LeetCode 3249 (Count the Number of Good Nodes) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3249 on DevExCode?

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

Back to LeetCode Solutions