[Expected Approach] - By using Recursion - O(n) Time and O(h) Auxiliary Space
The idea is to recursively calculate the size of tree. For each node (starting from root node), calculate the size of left subtree and right subtree and return the size of current subtree (size of left subtree + size of right subtree + 1).
Consider the following tree for example to understand the flow.
[Alternate Approach] - Using Breadth First Search(BFS) - O(n) Time and O(n) Space
The idea is to traverse the tree level by level using a queue. Start from the root node, and for each node, visit it, increment the count, and add its left and right children to the queue. Continue this process until all nodes are visited. The total count at the end gives the size of the tree.