Recall & Review
beginner
What is a binary tree?
A binary tree is a structure where each node has at most two children, called left and right.
Click to reveal answer
beginner
How do you count total nodes in a binary tree?
Count 1 for the current node plus the count of nodes in the left subtree and the right subtree.
Click to reveal answer
beginner
What is the base case when counting nodes recursively in a binary tree?
When the node is null (no node), return 0 because there are no nodes to count.
Click to reveal answer
intermediate
Why is recursion useful for counting nodes in a binary tree?
Because each node's count depends on the counts of its children, recursion naturally breaks the problem into smaller parts.
Click to reveal answer
intermediate
What is the time complexity of counting total nodes in a binary tree?
O(n), where n is the number of nodes, because each node is visited once.
Click to reveal answer
What should the function return when the current node is null?
✗ Incorrect
When the node is null, it means no node exists, so the count is 0.
If a binary tree has 5 nodes, what will the count function return?
✗ Incorrect
The function counts all nodes, so it returns 5 for 5 nodes.
Which traversal method is used when counting nodes recursively?
✗ Incorrect
Counting nodes only requires visiting all nodes once; any traversal visiting all nodes works.
What is the first step in counting nodes recursively?
✗ Incorrect
Always check if the current node is null to stop recursion.
What does the count function add to the counts of left and right subtrees?
✗ Incorrect
It adds 1 for the current node itself.
Explain how to count total nodes in a binary tree using recursion.
Think about how to break the problem into smaller parts.
You got /3 concepts.
Describe the time complexity of counting nodes in a binary tree and why.
Consider how many times the function runs for each node.
You got /3 concepts.