Advertisement

Count Nodes Equal to Average of Subtree - LeetCode 2265 Solution

Count Nodes Equal to Average of Subtree - Complete Solution Guide

Count Nodes Equal to Average of Subtree is LeetCode problem 2265, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree . Note: The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer. A subtree of root is a tree consisting of root and all of its descendants. Example 1: Input: root = [4,8,5,0,1,null,6] Output: 5 Explanation: For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. F

Detailed Explanation

The problem asks us to traverse a binary tree and, for each node, determine if the node's value is equal to the average of all the values in its subtree (including the node itself). The average is calculated by summing all the values in the subtree, dividing by the number of nodes in the subtree, and rounding down to the nearest integer. The input is the root of the binary tree, and the output is the number of nodes that satisfy this condition.

Solution Approach

The provided solutions use a post-order Depth-First Search (DFS) traversal. The `postOrder` function (or equivalent in other languages) recursively calculates the sum and the number of nodes in each subtree. For each node, it adds its own value to the sum of its left and right subtrees and increments the node count by the number of nodes in its left and right subtrees, plus one (for the node itself). It then calculates the average of the subtree and checks if the average equals the node's value. If it does, it increments a counter.

Step-by-Step Algorithm

  1. Step 1: Initialize a counter `count` to 0. This counter will store the number of nodes satisfying the average condition.
  2. Step 2: Define a recursive function `postOrder(node)` that takes a node as input and returns a tuple (or array) containing the sum of node values and the number of nodes in the subtree rooted at `node`.
  3. Step 3: Base case for recursion: If `node` is null, return (0, 0) as there are no nodes and the sum is 0.
  4. Step 4: Recursively call `postOrder(node.left)` to get the sum and count of the left subtree.
  5. Step 5: Recursively call `postOrder(node.right)` to get the sum and count of the right subtree.
  6. Step 6: Calculate the `currentSum` by adding the node's value, the sum of the left subtree, and the sum of the right subtree.
  7. Step 7: Calculate the `currentCount` by adding 1 (for the current node), the count of the left subtree, and the count of the right subtree.
  8. Step 8: Calculate the `average` by dividing `currentSum` by `currentCount` (integer division).
  9. Step 9: Check if `average` equals the node's value. If it does, increment the `count`.
  10. Step 10: Return `currentSum` and `currentCount` as a tuple (or array).
  11. Step 11: Call `postOrder(root)` to start the traversal from the root of the tree.
  12. Step 12: Return the final `count`.

Key Insights

  • Insight 1: A post-order traversal is ideal because we need to know the sum and count of the left and right subtrees before processing the current node.
  • Insight 2: The core logic involves recursively calculating the sum and count of each subtree and then checking if the current node's value matches the calculated average.
  • Insight 3: We can use a simple integer division to get the rounded-down average, as required by the problem statement.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

This problem involves: Tree, Depth-First Search, Binary Tree.

Companies

Asked at: INDmoney, Snowflake.