root of a binary tree. The subtree of a node is the node itself together with all of its descendants.
Count how many nodes hold a value equal to the average of the values in their subtree. The average is an integer one: the sum divided by the number of nodes, rounded down.root=tree_node([6, 2, 9, None, 3, 7]) → 3
Root 6: (6 + 2 + 9 + 3 + 7) / 5 = 27 / 5, which rounds down to 5, so no. Node 2: (2 + 3) / 2 = 2, yes. Node 9: (9 + 7) / 2 = 8, no. The leaves 3 and 7 always match their own average. The answer is 3.
root=tree_node([4, 4, 4, 4]) → 4
All values are equal, so every node matches the average of its subtree.
root=tree_node([0, 1]) → 2
The root's average is (0 + 1) / 2, which rounds down to 0, and the leaf's is 1. Both nodes count.
Node.val ≤ 1000root=tree_node([6, 2, 9, None, 3, 7])
3
Run checks the first 3 tests in your browser. Submit checks all 78 tests on the server, hidden ones included.