Untitled
unknown
plain_text
a year ago
687 B
6
Indexable
private int[] postorder(TreeNode node) {
if (node == null) return new int[]{0, 0}; // {sum, count}
// Recur for left and right subtrees
int[] left = postorder(node.left);
int[] right = postorder(node.right);
// Calculate the total sum and total count for the current subtree
int totalSum = left[0] + right[0] + node.val;
int totalCount = left[1] + right[1] + 1;
// Check if the node's value equals the average of its subtree
if (node.val == totalSum / totalCount) {
count++;
}
// Return the sum and count of the current subtree
return new int[]{totalSum, totalCount};
}Editor is loading...
Leave a Comment