0
0
DSA C++programming~5 mins

Count Total Nodes in Binary Tree in DSA C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 child.
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 in a binary tree recursively?
When the current node is null (no node), return 0 because there are no nodes to count.
Click to reveal answer
beginner
Why do we add 1 in the node counting function?
The 1 counts the current node itself before adding counts from left and right children.
Click to reveal answer
intermediate
What is the time complexity of counting nodes in a binary tree?
It is 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?
A1
Bnull
C-1
D0
Which traversal method is used to count total nodes in a binary tree?
AAny traversal that visits all nodes
BInorder traversal
CPostorder traversal
DPreorder traversal
If a binary tree has 5 nodes, what will the count function return?
A4
B5
C6
D0
What is the role of recursion in counting nodes?
ATo count nodes only in the left subtree
BTo count nodes only in the right subtree
CTo count nodes in both left and right subtrees and add current node
DTo avoid counting nodes
What happens if you forget to add 1 for the current node in the count?
AThe total count will be 0
BThe function will not work
CThe count will be double
DThe count will be correct
Explain how to count total nodes in a binary tree using recursion.
Think about visiting each node and adding counts from children.
You got /5 concepts.
    Describe the time complexity of counting nodes in a binary tree and why.
    Consider how many times the function runs per node.
    You got /4 concepts.