classSolution {
// Function to compute the size of the binary tree publicintsize(TreeNode root) {
if (root ==null) {
return 0;
}
return 1 + size(root.left) + size(root.right);
}
publicstaticvoidmain(String[] args) {
Solution solution =new Solution();
TreeNode root =new TreeNode(1);
root.left=new TreeNode(2);
root.right=new TreeNode(3);
System.out.println("Size of the tree: "+ solution.size(root)); // Output: 3 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
# Function to compute the size of the binary tree defsize(self, root):
if root isNone:
return0return1+ self.size(root.left) + self.size(root.right)
# Testing the solution if __name__ =="__main__":
solution = Solution()
root = Solution.TreeNode(1)
root.left = Solution.TreeNode(2)
root.right = Solution.TreeNode(3)
print("Size of the tree:", solution.size(root)) # Output: 3
⏰ Time complexity: O(n). Since we are visiting each node exactly once, the time complexity is linear in terms of the number of nodes (n).
🧺 Space complexity: O(h). The space complexity depends on the height (h) of the tree, since the recursive call stack will store functions up to the height of the tree.
Instead of using recursion (DFS), we can solve this problem iteratively using Breadth-First Search (BFS). BFS traverses the tree level by level using a queue, and while visiting each node, we count it. This approach avoids stack recursion and handles the traversal explicitly using a queue.
To find the size of the binary tree using BFS:
Use a queue to store nodes for level-order traversal.
Start by enqueueing the root node.
For each node in the queue:
Dequeue the node, visit it, and increment the size count.
If the node has a left child, enqueue it.
If the node has a right child, enqueue it.
Continue this process until the queue is empty.
Return the total count, which represents the size of the tree.
classSolution {
// Function to compute the size of the binary tree using BFSpublicintsize(TreeNode root) {
if (root ==null) {
return 0; // If the tree is empty }
Queue<TreeNode> queue =new LinkedList<>();
queue.add(root);
int size = 0;
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
size++; // Increment the size for the current node// Add children to the queueif (current.left!=null) {
queue.add(current.left);
}
if (current.right!=null) {
queue.add(current.right);
}
}
return size; // Total count of nodes }
publicstaticvoidmain(String[] args) {
Solution solution =new Solution();
TreeNode root =new TreeNode(1);
root.left=new TreeNode(2);
root.right=new TreeNode(3);
System.out.println("Size of the tree: "+ solution.size(root)); // Output: 3 }
}
classSolution:
# Function to compute the size of the binary tree using BFSdefsize(self, root):
ifnot root:
return0# If the tree is empty queue = deque([root]) # Initialize the queue with the root node size =0while queue:
current = queue.popleft() # Dequeue the current node size +=1# Increment the size for the current node# Add children to the queueif current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)
return size # Total count of nodes# Testing the solutionif __name__ =="__main__":
solution = Solution()
root = Solution.TreeNode(1)
root.left = Solution.TreeNode(2)
root.right = Solution.TreeNode(3)
print("Size of the tree:", solution.size(root)) # Output: 3