Recall & Review
beginner
What is a binary tree node?
A binary tree node is a structure that contains a value and two pointers/references: one to the left child and one to the right child. It can have zero, one, or two children.
Click to reveal answer
beginner
How do you count total nodes in a binary tree?
You count nodes by visiting each node once and adding 1 for the current node plus the counts from the left and right subtrees.
Click to reveal answer
beginner
What is the base case when counting nodes recursively in a binary tree?
The base case is when the current node is nil (no node), then the count is 0 because there is no node to count.
Click to reveal answer
intermediate
Why is recursion a good fit for counting nodes in a binary tree?
Because each node's count depends on the counts of its left and right children, recursion naturally breaks the problem into smaller similar problems.
Click to reveal answer
beginner
Write the formula for total nodes count in a binary tree node.
TotalNodes(node) = 0 if node is nil; otherwise 1 + TotalNodes(node.left) + TotalNodes(node.right).
Click to reveal answer
What should the count function return when the node is nil?
✗ Incorrect
When the node is nil, it means no node exists, so the count is 0.
Which traversal method is used to count all nodes in a binary tree?
✗ Incorrect
Any traversal that visits all nodes will count all nodes correctly.
What is the time complexity of counting nodes in a binary tree?
✗ Incorrect
Each node is visited once, so the time complexity is O(n), where n is the number of nodes.
If a binary tree has 5 nodes, what will the count function return?
✗ Incorrect
The count function returns the total number of nodes, which is 5.
Which of these is NOT needed to count nodes in a binary tree?
✗ Incorrect
Sorting nodes is not required to count them.
Explain how recursion helps in counting total nodes in a binary tree.
Think about how you break the problem into smaller parts.
You got /3 concepts.
Describe the steps to count total nodes in a binary tree using Go code.
Focus on the function structure and base case.
You got /3 concepts.