Grokking Coding Mock Interview
Ask Author
Back to course home

0% completed

Vote For New Content

Solution: Count Nodes Equal to Average of Subtree
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given the root node of a binary tree, count how many nodes have a value that matches the average of all values in their subtree.

Note:

  • The average should be calculated by dividing the total sum of values in the subtree by the number of nodes in it, using integer division (rounding down).
  • A subtree of a particular node includes the node itself and all of its children in the tree.

Examples

Example 1

  • Input: root = [5]
  • Output: 1
  • Explanation:

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible