Untitled

 avatar
unknown
plain_text
a month ago
687 B
1
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};
    }
Leave a Comment