CNTND Editorial

Problem Explanation

We have to find and return the count of nodes in every subtree of a given binary tree.

Approach

We can use DFS to traverse the binary tree and return the count of the subtree. Then the count of nodes in the current subtree will be the sum of the count of the left subtree, the count of the right subtree and the current node which will be 1. The base cases will be returning 0 when the current node is NULL. We save the count in a map and then push all the counts in a vector.