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?
A0
B1
C-1
Dnil
✗ 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?
AAny traversal that visits all nodes
BPreorder traversal
CInorder traversal
DPostorder traversal
✗ Incorrect
Any traversal that visits all nodes will count all nodes correctly.
What is the time complexity of counting nodes in a binary tree?
AO(log n)
BO(n)
CO(n^2)
DO(1)
✗ 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?
A4
B0
C5
D6
✗ 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?
ACheck if node is nil
BCount left subtree nodes
CCount right subtree nodes
DSort the nodes
✗ 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.
Practice
(1/5)
1. What does the function to count total nodes in a binary tree do?
easy
A. It counts only the leaf nodes of the tree.
B. It counts all nodes including root, left, and right children.
C. It counts only the nodes on the left side of the tree.
D. It counts only the nodes on the right side of the tree.
Solution
Step 1: Understand node counting in binary tree
Counting total nodes means including every node: root, left child, and right child recursively.
Step 2: Compare options with definition
Only the option that says 'It counts all nodes including root, left, and right children.' includes all nodes, others limit to parts of the tree.
Final Answer:
It counts all nodes including root, left, and right children. -> Option B
Quick Check:
Total nodes = all nodes in tree [OK]
Hint: Count every node, not just leaves or sides [OK]
Common Mistakes:
Counting only leaf nodes
Ignoring root node
Counting only one subtree
2. Which of the following Go function signatures correctly defines a function to count nodes in a binary tree with root type *Node?
easy
A. func countNodes(root *Node) string
B. func countNodes(root Node) int
C. func countNodes(root *Node) int
D. func countNodes(root Node) bool
Solution
Step 1: Identify correct parameter type
Binary tree root is usually a pointer to Node, so *Node is correct.
Step 2: Identify correct return type
Counting nodes returns an integer, so return type int is correct.
Final Answer:
func countNodes(root *Node) int -> Option C
Quick Check:
Pointer parameter and int return type [OK]
Hint: Use pointer receiver and int return for count function [OK]
Common Mistakes:
Passing Node by value instead of pointer
Returning wrong type like string or bool
Missing pointer in parameter
3. What is the output of the following Go code that counts nodes in a binary tree?
package main
import "fmt"
type Node struct {
Val int
Left *Node
Right *Node
}
func countNodes(root *Node) int {
if root == nil {
return 0
}
return 1 + countNodes(root.Left) + countNodes(root.Right)
}
func main() {
root := &Node{Val: 1}
root.Left = &Node{Val: 2}
root.Right = &Node{Val: 3}
root.Left.Left = &Node{Val: 4}
fmt.Println(countNodes(root))
}